Merge commit 'b7b1bf9166ac3102c401295fdd5d4933c512aa50'
authorMichael Niedermayer <michaelni@gmx.at>
Thu, 7 Aug 2014 19:16:06 +0000 (21:16 +0200)
committerMichael Niedermayer <michaelni@gmx.at>
Thu, 7 Aug 2014 19:16:06 +0000 (21:16 +0200)
* commit 'b7b1bf9166ac3102c401295fdd5d4933c512aa50':
  mpegts: K&R formatting cosmetics

Conflicts:
libavformat/mpegtsenc.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
libavformat/mpegtsenc.c

@@@ -19,6 -19,6 +19,7 @@@
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   */
  
++#include "libavutil/avassert.h"
  #include "libavutil/bswap.h"
  #include "libavutil/crc.h"
  #include "libavutil/dict.h"
@@@ -167,9 -165,9 +171,9 @@@ static int mpegts_write_section1(MpegTS
      tot_len = 3 + 5 + len + 4;
      /* check if not too big */
      if (tot_len > 1024)
 -        return -1;
 +        return AVERROR_INVALIDDATA;
  
-     q = section;
+     q    = section;
      *q++ = tid;
      put16(&q, flags | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
      put16(&q, id);
@@@ -300,32 -289,18 +306,32 @@@ static int mpegts_write_pmt(AVFormatCon
          q += 2; /* patched after */
  
          /* write optional descriptors here */
-         switch(st->codec->codec_type) {
+         switch (st->codec->codec_type) {
          case AVMEDIA_TYPE_AUDIO:
-             if(st->codec->codec_id==AV_CODEC_ID_EAC3){
++            if (st->codec->codec_id==AV_CODEC_ID_EAC3) {
 +                *q++=0x7a; // EAC3 descriptor see A038 DVB SI
 +                *q++=1; // 1 byte, all flags sets to 0
 +                *q++=0; // omit all fields...
 +            }
-             if(st->codec->codec_id==AV_CODEC_ID_S302M){
++            if (st->codec->codec_id==AV_CODEC_ID_S302M) {
 +                *q++ = 0x05; /* MPEG-2 registration descriptor*/
 +                *q++ = 4;
 +                *q++ = 'B';
 +                *q++ = 'S';
 +                *q++ = 'S';
 +                *q++ = 'D';
 +            }
 +
              if (lang) {
                  char *p;
                  char *next = lang->value;
                  uint8_t *len_ptr;
  
-                 *q++ = 0x0a; /* ISO 639 language descriptor */
-                 len_ptr = q++;
+                 *q++     = 0x0a; /* ISO 639 language descriptor */
+                 len_ptr  = q++;
                  *len_ptr = 0;
  
 -                for (p = lang->value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
 +                for (p = lang->value; next && *len_ptr < 255 / 4 * 4 && q - data < sizeof(data) - 4; p = next + 1) {
                      next = strchr(p, ',');
                      if (strlen(p) != 3 && (!next || next != p + 3))
                          continue; /* not a 3-letter code */
              }
              break;
          case AVMEDIA_TYPE_SUBTITLE:
-             {
-                 const char default_language[] = "und";
-                 const char *language = lang && strlen(lang->value) >= 3 ? lang->value : default_language;
-                 if (st->codec->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
-                     uint8_t *len_ptr;
-                     int extradata_copied = 0;
-                     *q++ = 0x59; /* subtitling_descriptor */
-                     len_ptr = q++;
-                     while (strlen(language) >= 3 && (sizeof(data) - (q - data)) >= 8) { /* 8 bytes per DVB subtitle substream data */
-                         *q++ = *language++;
-                         *q++ = *language++;
-                         *q++ = *language++;
-                         /* Skip comma */
-                         if (*language != '\0')
-                             language++;
-                         if (st->codec->extradata_size - extradata_copied >= 5) {
-                             *q++ = st->codec->extradata[extradata_copied + 4]; /* subtitling_type */
-                             memcpy(q, st->codec->extradata + extradata_copied, 4); /* composition_page_id and ancillary_page_id */
-                             extradata_copied += 5;
-                             q += 4;
-                         } else {
-                             /* subtitling_type:
-                              * 0x10 - normal with no monitor aspect ratio criticality
-                              * 0x20 - for the hard of hearing with no monitor aspect ratio criticality */
-                             *q++ = (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED) ? 0x20 : 0x10;
-                             if ((st->codec->extradata_size == 4) && (extradata_copied == 0)) {
-                                 /* support of old 4-byte extradata format */
-                                 memcpy(q, st->codec->extradata, 4); /* composition_page_id and ancillary_page_id */
-                                 extradata_copied += 4;
-                                 q += 4;
-                             } else {
-                                 put16(&q, 1); /* composition_page_id */
-                                 put16(&q, 1); /* ancillary_page_id */
-                             }
-                         }
-                     }
-                     *len_ptr = q - len_ptr - 1;
-                 } else if (st->codec->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
-                     uint8_t *len_ptr = NULL;
-                     int extradata_copied = 0;
-                     /* The descriptor tag. teletext_descriptor */
-                     *q++ = 0x56;
-                     len_ptr = q++;
-                     while (strlen(language) >= 3 && q - data < sizeof(data) - 6) {
-                         *q++ = *language++;
-                         *q++ = *language++;
-                         *q++ = *language++;
-                         /* Skip comma */
-                         if (*language != '\0')
-                             language++;
-                         if (st->codec->extradata_size - 1 > extradata_copied) {
-                             memcpy(q, st->codec->extradata + extradata_copied, 2);
-                             extradata_copied += 2;
-                             q += 2;
-                         } else {
-                             /* The Teletext descriptor:
-                              * teletext_type: This 5-bit field indicates the type of Teletext page indicated. (0x01 Initial Teletext page)
-                              * teletext_magazine_number: This is a 3-bit field which identifies the magazine number.
-                              * teletext_page_number: This is an 8-bit field giving two 4-bit hex digits identifying the page number. */
-                             *q++ = 0x08;
-                             *q++ = 0x00;
-                         }
-                     }
-                     *len_ptr = q - len_ptr - 1;
-                  }
+         {
 -            const char *language;
 -            language = lang && strlen(lang->value) == 3 ? lang->value : "eng";
 -            *q++ = 0x59;
 -            *q++ = 8;
 -            *q++ = language[0];
 -            *q++ = language[1];
 -            *q++ = language[2];
 -            *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
 -            if (st->codec->extradata_size == 4) {
 -                memcpy(q, st->codec->extradata, 4);
 -                q += 4;
 -            } else {
 -                put16(&q, 1);     /* page id */
 -                put16(&q, 1);     /* ancillary page id */
++           const char default_language[] = "und";
++           const char *language = lang && strlen(lang->value) >= 3 ? lang->value : default_language;
++
++           if (st->codec->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
++               uint8_t *len_ptr;
++               int extradata_copied = 0;
++
++               *q++ = 0x59; /* subtitling_descriptor */
++               len_ptr = q++;
++
++               while (strlen(language) >= 3 && (sizeof(data) - (q - data)) >= 8) { /* 8 bytes per DVB subtitle substream data */
++                   *q++ = *language++;
++                   *q++ = *language++;
++                   *q++ = *language++;
++                   /* Skip comma */
++                   if (*language != '\0')
++                       language++;
++
++                   if (st->codec->extradata_size - extradata_copied >= 5) {
++                       *q++ = st->codec->extradata[extradata_copied + 4]; /* subtitling_type */
++                       memcpy(q, st->codec->extradata + extradata_copied, 4); /* composition_page_id and ancillary_page_id */
++                       extradata_copied += 5;
++                       q += 4;
++                   } else {
++                       /* subtitling_type:
++                        * 0x10 - normal with no monitor aspect ratio criticality
++                        * 0x20 - for the hard of hearing with no monitor aspect ratio criticality */
++                       *q++ = (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED) ? 0x20 : 0x10;
++                       if ((st->codec->extradata_size == 4) && (extradata_copied == 0)) {
++                           /* support of old 4-byte extradata format */
++                           memcpy(q, st->codec->extradata, 4); /* composition_page_id and ancillary_page_id */
++                           extradata_copied += 4;
++                           q += 4;
++                       } else {
++                           put16(&q, 1); /* composition_page_id */
++                           put16(&q, 1); /* ancillary_page_id */
++                       }
++                   }
++               }
++
++               *len_ptr = q - len_ptr - 1;
++           } else if (st->codec->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
++               uint8_t *len_ptr = NULL;
++               int extradata_copied = 0;
++
++               /* The descriptor tag. teletext_descriptor */
++               *q++ = 0x56;
++               len_ptr = q++;
++
++               while (strlen(language) >= 3 && q - data < sizeof(data) - 6) {
++                   *q++ = *language++;
++                   *q++ = *language++;
++                   *q++ = *language++;
++                   /* Skip comma */
++                   if (*language != '\0')
++                       language++;
++
++                   if (st->codec->extradata_size - 1 > extradata_copied) {
++                       memcpy(q, st->codec->extradata + extradata_copied, 2);
++                       extradata_copied += 2;
++                       q += 2;
++                   } else {
++                       /* The Teletext descriptor:
++                        * teletext_type: This 5-bit field indicates the type of Teletext page indicated. (0x01 Initial Teletext page)
++                        * teletext_magazine_number: This is a 3-bit field which identifies the magazine number.
++                        * teletext_page_number: This is an 8-bit field giving two 4-bit hex digits identifying the page number. */
++                       *q++ = 0x08;
++                       *q++ = 0x00;
++                   }
++               }
++
++               *len_ptr = q - len_ptr - 1;
              }
-             break;
+         }
+         break;
          case AVMEDIA_TYPE_VIDEO:
              if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
                  *q++ = 0x05; /*MPEG-2 registration descriptor*/
@@@ -582,25 -468,29 +587,29 @@@ static int mpegts_write_header(AVFormat
      title = av_dict_get(s->metadata, "service_name", NULL, 0);
      if (!title)
          title = av_dict_get(s->metadata, "title", NULL, 0);
-     service_name = title ? title->value : DEFAULT_SERVICE_NAME;
-     provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
+     service_name  = title ? title->value : DEFAULT_SERVICE_NAME;
+     provider      = av_dict_get(s->metadata, "service_provider", NULL, 0);
      provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
-     service = mpegts_add_service(ts, ts->service_id, provider_name, service_name);
+     service       = mpegts_add_service(ts, ts->service_id,
+                                        provider_name, service_name);
      service->pmt.write_packet = section_write_packet;
-     service->pmt.opaque = s;
-     service->pmt.cc = 15;
+     service->pmt.opaque       = s;
+     service->pmt.cc           = 15;
  
-     ts->pat.pid = PAT_PID;
-     ts->pat.cc = 15; // Initialize at 15 so that it wraps and be equal to 0 for the first packet we write
+     ts->pat.pid          = PAT_PID;
+     /* Initialize at 15 so that it wraps and is equal to 0 for the
+      * first packet we write. */
+     ts->pat.cc           = 15;
      ts->pat.write_packet = section_write_packet;
-     ts->pat.opaque = s;
+     ts->pat.opaque       = s;
  
-     ts->sdt.pid = SDT_PID;
-     ts->sdt.cc = 15;
+     ts->sdt.pid          = SDT_PID;
+     ts->sdt.cc           = 15;
      ts->sdt.write_packet = section_write_packet;
-     ts->sdt.opaque = s;
+     ts->sdt.opaque       = s;
  
 -    pids = av_malloc(s->nb_streams * sizeof(*pids));
 +    pids = av_malloc_array(s->nb_streams, sizeof(*pids));
      if (!pids)
          return AVERROR(ENOMEM);
  
  
      if (ts->mux_rate > 1) {
          service->pcr_packet_period = (ts->mux_rate * ts->pcr_period) /
-             (TS_PACKET_SIZE * 8 * 1000);
+                                      (TS_PACKET_SIZE * 8 * 1000);
          ts->sdt_packet_period      = (ts->mux_rate * SDT_RETRANS_TIME) /
-             (TS_PACKET_SIZE * 8 * 1000);
+                                      (TS_PACKET_SIZE * 8 * 1000);
          ts->pat_packet_period      = (ts->mux_rate * PAT_RETRANS_TIME) /
-             (TS_PACKET_SIZE * 8 * 1000);
+                                      (TS_PACKET_SIZE * 8 * 1000);
  
-         if(ts->copyts < 1)
 -        ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
++        if (ts->copyts < 1)
 +            ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
      } else {
 -        /* Arbitrary values, PAT/PMT could be written on key frames */
 +        /* Arbitrary values, PAT/PMT will also be written on video key frames */
          ts->sdt_packet_period = 200;
          ts->pat_packet_period = 40;
          if (pcr_st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
              service->pcr_packet_period =
                  ts_st->user_tb.den / (10 * ts_st->user_tb.num);
          }
-         if(!service->pcr_packet_period)
++        if (!service->pcr_packet_period)
 +            service->pcr_packet_period = 1;
      }
  
      // output a PCR as soon as possible
@@@ -780,12 -660,11 +792,11 @@@ static void retransmit_si_info(AVFormat
          ts->sdt_packet_count = 0;
          mpegts_write_sdt(s);
      }
 -    if (++ts->pat_packet_count == ts->pat_packet_period) {
 +    if (++ts->pat_packet_count == ts->pat_packet_period || force_pat) {
          ts->pat_packet_count = 0;
          mpegts_write_pat(s);
-         for(i = 0; i < ts->nb_services; i++) {
+         for (i = 0; i < ts->nb_services; i++)
              mpegts_write_pmt(s, ts->services[i]);
-         }
      }
  }
  
@@@ -974,37 -854,27 +985,37 @@@ static void mpegts_write_pes(AVFormatCo
              *q++ = 0x00;
              *q++ = 0x00;
              *q++ = 0x01;
 -            private_code = 0;
 +            is_dvb_subtitle = 0;
 +            is_dvb_teletext = 0;
              if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
-                 if (st->codec->codec_id == AV_CODEC_ID_DIRAC) {
+                 if (st->codec->codec_id == AV_CODEC_ID_DIRAC)
                      *q++ = 0xfd;
-                 else
+                 else
                      *q++ = 0xe0;
              } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
                         (st->codec->codec_id == AV_CODEC_ID_MP2 ||
                          st->codec->codec_id == AV_CODEC_ID_MP3 ||
                          st->codec->codec_id == AV_CODEC_ID_AAC)) {
                  *q++ = 0xc0;
 +            } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
 +                        st->codec->codec_id == AV_CODEC_ID_AC3 &&
 +                        ts->m2ts_mode) {
 +                *q++ = 0xfd;
              } else {
                  *q++ = 0xbd;
-                 if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
 -                if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
 -                    private_code = 0x20;
++                if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
 +                    if (st->codec->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
 +                        is_dvb_subtitle = 1;
 +                    } else if (st->codec->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
 +                        is_dvb_teletext = 1;
 +                    }
 +                }
              }
              header_len = 0;
-             flags = 0;
+             flags      = 0;
              if (pts != AV_NOPTS_VALUE) {
                  header_len += 5;
-                 flags |= 0x80;
+                 flags      |= 0x80;
              }
              if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
                  header_len += 5;
                  st->codec->codec_id == AV_CODEC_ID_DIRAC) {
                  /* set PES_extension_flag */
                  pes_extension = 1;
-                 flags |= 0x01;
+                 flags        |= 0x01;
  
-                 /*
-                 * One byte for PES2 extension flag +
-                 * one byte for extension length +
-                 * one byte for extension id
-                 */
+                 /* One byte for PES2 extension flag +
+                  * one byte for extension length +
+                  * one byte for extension id */
                  header_len += 3;
              }
 +            /* for Blu-ray AC3 Audio the PES Extension flag should be as follow
 +             * otherwise it will not play sound on blu-ray
 +             */
 +            if (ts->m2ts_mode &&
 +                st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
 +                st->codec->codec_id == AV_CODEC_ID_AC3) {
 +                        /* set PES_extension_flag */
 +                        pes_extension = 1;
 +                        flags |= 0x01;
 +                        header_len += 3;
 +            }
 +            if (is_dvb_teletext) {
 +                pes_header_stuffing_bytes = 0x24 - header_len;
 +                header_len = 0x24;
 +            }
              len = payload_size + header_len + 3;
 -            if (private_code != 0)
 -                len++;
 +            /* 3 extra bytes should be added to DVB subtitle payload: 0x20 0x00 at the beginning and trailing 0xff */
 +            if (is_dvb_subtitle) {
 +                len += 3;
 +                payload_size++;
 +            }
              if (len > 0xffff)
                  len = 0;
 +            if (ts->omit_video_pes_length && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
 +                len = 0;
 +            }
              *q++ = len >> 8;
              *q++ = len;
-             val = 0x80;
+             val  = 0x80;
 -            /* data alignment indicator is required for subtitle data */
 -            if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
 +            /* data alignment indicator is required for subtitle and data streams */
 +            if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE || st->codec->codec_type == AVMEDIA_TYPE_DATA)
                  val |= 0x04;
              *q++ = val;
              *q++ = flags;
              }
              if (pes_extension && st->codec->codec_id == AV_CODEC_ID_DIRAC) {
                  flags = 0x01;  /* set PES_extension_flag_2 */
-                 *q++ = flags;
-                 *q++ = 0x80 | 0x01;  /* marker bit + extension length */
-                 /*
-                 * Set the stream id extension flag bit to 0 and
-                 * write the extended stream id
-                 */
+                 *q++  = flags;
+                 *q++  = 0x80 | 0x01; /* marker bit + extension length */
+                 /* Set the stream ID extension flag bit to 0 and
+                  * write the extended stream ID. */
                  *q++ = 0x00 | 0x60;
              }
 -            if (private_code != 0)
 -                *q++ = private_code;
 +            /* For Blu-ray AC3 Audio Setting extended flags */
 +          if (ts->m2ts_mode &&
 +              pes_extension &&
 +              st->codec->codec_id == AV_CODEC_ID_AC3) {
 +                      flags = 0x01; /* set PES_extension_flag_2 */
 +                      *q++ = flags;
 +                      *q++ = 0x80 | 0x01; /* marker bit + extension length */
 +                      *q++ = 0x00 | 0x71; /* for AC3 Audio (specifically on blue-rays) */
 +              }
 +
 +
 +            if (is_dvb_subtitle) {
 +                /* First two fields of DVB subtitles PES data:
 +                 * data_identifier: for DVB subtitle streams shall be coded with the value 0x20
 +                 * subtitle_stream_id: for DVB subtitle stream shall be identified by the value 0x00 */
 +                *q++ = 0x20;
 +                *q++ = 0x00;
 +            }
 +            if (is_dvb_teletext) {
 +                memset(q, 0xff, pes_header_stuffing_bytes);
 +                q += pes_header_stuffing_bytes;
 +            }
              is_start = 0;
          }
          /* header size */
                  }
              }
          }
 -        memcpy(buf + TS_PACKET_SIZE - len, payload, len);
 +
 +        if (is_dvb_subtitle && payload_size == len) {
 +            memcpy(buf + TS_PACKET_SIZE - len, payload, len - 1);
 +            buf[TS_PACKET_SIZE - 1] = 0xff; /* end_of_PES_data_field_marker: an 8-bit field with fixed contents 0xff for DVB subtitle */
 +        } else {
 +            memcpy(buf + TS_PACKET_SIZE - len, payload, len);
 +        }
 +
-         payload += len;
+         payload      += len;
          payload_size -= len;
 +        mpegts_prefix_m2ts_header(s);
          avio_write(s->pb, buf, TS_PACKET_SIZE);
      }
 -    avio_flush(s->pb);
 +    ts_st->prev_payload_key = key;
 +}
 +
 +int ff_check_h264_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
 +{
 +    if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
 +        if (!st->nb_frames) {
 +            av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
 +                   "no startcode found, use the h264_mp4toannexb bitstream filter (-bsf h264_mp4toannexb)\n");
 +            return AVERROR(EINVAL);
 +        }
 +        av_log(s, AV_LOG_WARNING, "H.264 bitstream error, startcode missing\n");
 +    }
 +    return 0;
  }
  
  static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
  {
      AVStream *st = s->streams[pkt->stream_index];
      int size = pkt->size;
-     uint8_t *buf= pkt->data;
-     uint8_t *data= NULL;
+     uint8_t *buf = pkt->data;
+     uint8_t *data = NULL;
      MpegTSWrite *ts = s->priv_data;
      MpegTSWriteStream *ts_st = st->priv_data;
-     const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE)*2;
 -    const uint64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
 -    int64_t dts = AV_NOPTS_VALUE, pts = AV_NOPTS_VALUE;
++    const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
 +    int64_t dts = pkt->dts, pts = pkt->pts;
  
      if (ts->reemit_pat_pmt) {
-         av_log(s, AV_LOG_WARNING, "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
+         av_log(s, AV_LOG_WARNING,
+                "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
          ts->reemit_pat_pmt = 0;
-         ts->flags |= MPEGTS_FLAG_REEMIT_PAT_PMT;
+         ts->flags         |= MPEGTS_FLAG_REEMIT_PAT_PMT;
      }
  
      if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
          ts->pat_packet_count = ts->pat_packet_period - 1;
          ts->sdt_packet_count = ts->sdt_packet_period - 1;
-         ts->flags &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;
+         ts->flags           &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;
      }
  
-     if(ts->copyts < 1){
 -    if (pkt->pts != AV_NOPTS_VALUE)
 -        pts = pkt->pts + delay;
 -    if (pkt->dts != AV_NOPTS_VALUE)
 -        dts = pkt->dts + delay;
++    if (ts->copyts < 1) {
 +        if (pts != AV_NOPTS_VALUE)
 +            pts += delay;
 +        if (dts != AV_NOPTS_VALUE)
 +            dts += delay;
 +    }
  
      if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
 -        av_log(s, AV_LOG_ERROR, "first pts value must set\n");
 -        return AVERROR(EINVAL);
 +        av_log(s, AV_LOG_ERROR, "first pts value must be set\n");
 +        return AVERROR_INVALIDDATA;
      }
      ts_st->first_pts_check = 0;
  
      if (st->codec->codec_id == AV_CODEC_ID_H264) {
-         const uint8_t *p = buf, *buf_end = p+size;
+         const uint8_t *p = buf, *buf_end = p + size;
          uint32_t state = -1;
 -
 -        if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
 -            av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
 -                   "no startcode found, use -bsf h264_mp4toannexb\n");
 -            return AVERROR(EINVAL);
 -        }
 +        int ret = ff_check_h264_startcode(s, st, pkt);
 +        if (ret < 0)
 +            return ret;
  
          do {
              p = avpriv_find_start_code(p, buf_end, &state);
  
              if (!ts_st->amux) {
                  av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "
-                        "and extradata missing\n");
+                                         "and extradata missing\n");
 -                return AVERROR(EINVAL);
 +                return AVERROR_INVALIDDATA;
              }
  
              av_init_packet(&pkt2);
              pkt2.data = pkt->data;
              pkt2.size = pkt->size;
 +            av_assert0(pkt->dts != AV_NOPTS_VALUE);
 +            pkt2.dts = av_rescale_q(pkt->dts, st->time_base, ts_st->amux->streams[0]->time_base);
              ret = avio_open_dyn_buf(&ts_st->amux->pb);
              if (ret < 0)
                  return AVERROR(ENOMEM);
          }
      }
  
 -    if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
 +    if (pkt->dts != AV_NOPTS_VALUE) {
 +        int i;
-         for(i=0; i<s->nb_streams; i++){
++        for(i=0; i<s->nb_streams; i++) {
 +            AVStream *st2 = s->streams[i];
 +            MpegTSWriteStream *ts_st2 = st2->priv_data;
-             if(   ts_st2->payload_size
-                && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)){
++            if (   ts_st2->payload_size
++               && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)) {
 +                mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
 +                                ts_st2->payload_pts, ts_st2->payload_dts,
 +                                ts_st2->payload_flags & AV_PKT_FLAG_KEY);
 +                ts_st2->payload_size = 0;
 +            }
 +        }
 +    }
 +
 +    if (ts_st->payload_size && ts_st->payload_size + size > ts->pes_payload_size) {
 +        mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
 +                         ts_st->payload_pts, ts_st->payload_dts,
 +                         ts_st->payload_flags & AV_PKT_FLAG_KEY);
 +        ts_st->payload_size = 0;
 +    }
 +
 +    if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO || size > ts->pes_payload_size) {
 +        av_assert0(!ts_st->payload_size);
          // for video and subtitle, write a single pes packet
-         mpegts_write_pes(s, st, buf, size, pts, dts, pkt->flags & AV_PKT_FLAG_KEY);
+         mpegts_write_pes(s, st, buf, size, pts, dts,
+                          pkt->flags & AV_PKT_FLAG_KEY);
          av_free(data);
          return 0;
      }
  
 -    if (ts_st->payload_size + size > ts->pes_payload_size) {
 -        if (ts_st->payload_size) {
 -            mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
 -                             ts_st->payload_pts, ts_st->payload_dts,
 -                             ts_st->payload_flags & AV_PKT_FLAG_KEY);
 -            ts_st->payload_size = 0;
 -        }
 -        if (size > ts->pes_payload_size) {
 -            mpegts_write_pes(s, st, buf, size, pts, dts,
 -                             pkt->flags & AV_PKT_FLAG_KEY);
 -            av_free(data);
 -            return 0;
 -        }
 -    }
 -
      if (!ts_st->payload_size) {
-         ts_st->payload_pts = pts;
-         ts_st->payload_dts = dts;
+         ts_st->payload_pts   = pts;
+         ts_st->payload_dts   = dts;
          ts_st->payload_flags = pkt->flags;
      }
  
@@@ -1356,40 -1155,42 +1366,54 @@@ static int mpegts_write_end(AVFormatCon
  
  static const AVOption options[] = {
      { "mpegts_transport_stream_id", "Set transport_stream_id field.",
-       offsetof(MpegTSWrite, transport_stream_id), AV_OPT_TYPE_INT, {.i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
+       offsetof(MpegTSWrite, transport_stream_id), AV_OPT_TYPE_INT,
+       { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
      { "mpegts_original_network_id", "Set original_network_id field.",
-       offsetof(MpegTSWrite, original_network_id), AV_OPT_TYPE_INT, {.i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
+       offsetof(MpegTSWrite, original_network_id), AV_OPT_TYPE_INT,
+       { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
      { "mpegts_service_id", "Set service_id field.",
-       offsetof(MpegTSWrite, service_id), AV_OPT_TYPE_INT, {.i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
+       offsetof(MpegTSWrite, service_id), AV_OPT_TYPE_INT,
+       { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
      { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
-       offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT, {.i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM},
+       offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT,
 -      { .i64 = 0x1000 }, 0x1000, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
++      { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
      { "mpegts_start_pid", "Set the first pid.",
-       offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT, {.i64 = 0x0100 }, 0x0100, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM},
-     {"mpegts_m2ts_mode", "Enable m2ts mode.",
-         offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_INT, {.i64 = -1 },
-         -1,1, AV_OPT_FLAG_ENCODING_PARAM},
-     { "muxrate", NULL, offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT, {.i64 = 1}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
+       offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT,
+       { .i64 = 0x0100 }, 0x0100, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM },
++    { "mpegts_m2ts_mode", "Enable m2ts mode.",
++      offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_INT,
++      { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
+     { "muxrate", NULL,
+       offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT,
+       { .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
      { "pes_payload_size", "Minimum PES packet payload in bytes",
-       offsetof(MpegTSWrite, pes_payload_size), AV_OPT_TYPE_INT, {.i64 = DEFAULT_PES_PAYLOAD_SIZE}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
-     { "mpegts_flags", "MPEG-TS muxing flags", offsetof(MpegTSWrite, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX,
+       offsetof(MpegTSWrite, pes_payload_size), AV_OPT_TYPE_INT,
+       { .i64 = DEFAULT_PES_PAYLOAD_SIZE }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
+     { "mpegts_flags", "MPEG-TS muxing flags",
+       offsetof(MpegTSWrite, flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX,
        AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
      { "resend_headers", "Reemit PAT/PMT before writing the next packet",
-       0, AV_OPT_TYPE_CONST, {.i64 = MPEGTS_FLAG_REEMIT_PAT_PMT}, 0, INT_MAX,
-       AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags"},
+       0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_REEMIT_PAT_PMT }, 0, INT_MAX,
+       AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
      { "latm", "Use LATM packetization for AAC",
-       0, AV_OPT_TYPE_CONST, {.i64 = MPEGTS_FLAG_AAC_LATM}, 0, INT_MAX,
-       AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags"},
+       0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_AAC_LATM }, 0, INT_MAX,
+       AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
      // backward compatibility
      { "resend_headers", "Reemit PAT/PMT before writing the next packet",
-       offsetof(MpegTSWrite, reemit_pat_pmt), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
+       offsetof(MpegTSWrite, reemit_pat_pmt), AV_OPT_TYPE_INT,
+       { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
 +    { "mpegts_copyts", "don't offset dts/pts",
-       offsetof(MpegTSWrite, copyts), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
++      offsetof(MpegTSWrite, copyts), AV_OPT_TYPE_INT,
++      { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
 +    { "tables_version", "set PAT, PMT and SDT version",
-       offsetof(MpegTSWrite, tables_version), AV_OPT_TYPE_INT, {.i64=0}, 0, 31, AV_OPT_FLAG_ENCODING_PARAM},
++      offsetof(MpegTSWrite, tables_version), AV_OPT_TYPE_INT,
++      { .i64 = 0 }, 0, 31, AV_OPT_FLAG_ENCODING_PARAM },
 +    { "omit_video_pes_length", "Omit the PES packet length for video packets",
-       offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
++      offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_INT,
++      { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
      { "pcr_period", "PCR retransmission time",
-       offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_INT, { .i64 = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
+       offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_INT,
+       { .i64 = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
      { NULL },
  };
  
@@@ -1401,16 -1202,16 +1425,16 @@@ static const AVClass mpegts_muxer_clas
  };
  
  AVOutputFormat ff_mpegts_muxer = {
-     .name              = "mpegts",
-     .long_name         = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
-     .mime_type         = "video/MP2T",
-     .extensions        = "ts,m2t,m2ts,mts",
-     .priv_data_size    = sizeof(MpegTSWrite),
-     .audio_codec       = AV_CODEC_ID_MP2,
-     .video_codec       = AV_CODEC_ID_MPEG2VIDEO,
-     .write_header      = mpegts_write_header,
-     .write_packet      = mpegts_write_packet,
-     .write_trailer     = mpegts_write_end,
-     .flags             = AVFMT_ALLOW_FLUSH,
-     .priv_class        = &mpegts_muxer_class,
+     .name           = "mpegts",
+     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
 -    .mime_type      = "video/x-mpegts",
 -    .extensions     = "ts,m2t",
++    .mime_type      = "video/MP2T",
++    .extensions     = "ts,m2t,m2ts,mts",
+     .priv_data_size = sizeof(MpegTSWrite),
+     .audio_codec    = AV_CODEC_ID_MP2,
+     .video_codec    = AV_CODEC_ID_MPEG2VIDEO,
+     .write_header   = mpegts_write_header,
+     .write_packet   = mpegts_write_packet,
+     .write_trailer  = mpegts_write_end,
+     .flags          = AVFMT_ALLOW_FLUSH,
+     .priv_class     = &mpegts_muxer_class,
  };