]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mpegts.c
Move av_tempfile() to libxvidff.c as only the xvid wrapper needs it
[ffmpeg] / libavformat / mpegts.c
index f20c363b459d727d920b153086d10c8fc0a14e5d..d9ee987e1d85b8d7ba5e26a11e7839b4fbf32ba1 100644 (file)
@@ -85,7 +85,7 @@ struct Program {
     unsigned int pids[MAX_PIDS_PER_PROGRAM];
 };
 
-typedef struct MpegTSContext {
+struct MpegTSContext {
     /* user data */
     AVFormatContext *stream;
     /** raw packet size, including FEC if present            */
@@ -120,7 +120,7 @@ typedef struct MpegTSContext {
 
     /** filters for various streams specified by PMT + for the PAT and PMT */
     MpegTSFilter *pids[NB_PID_MAX];
-} MpegTSContext;
+};
 
 /* TS stream handling */
 
@@ -239,7 +239,7 @@ static int discard_pid(MpegTSContext *ts, unsigned int pid)
 }
 
 /**
- *  Assembles PES packets out of TS packets, and then calls the "section_cb"
+ *  Assemble PES packets out of TS packets, and then call the "section_cb"
  *  function when they are complete.
  */
 static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
@@ -486,54 +486,54 @@ static int parse_section_header(SectionHeader *h,
 
 typedef struct {
     uint32_t stream_type;
-    enum CodecType codec_type;
+    enum AVMediaType codec_type;
     enum CodecID codec_id;
 } StreamType;
 
 static const StreamType ISO_types[] = {
-    { 0x01, CODEC_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
-    { 0x02, CODEC_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
-    { 0x03, CODEC_TYPE_AUDIO,        CODEC_ID_MP3 },
-    { 0x04, CODEC_TYPE_AUDIO,        CODEC_ID_MP3 },
-    { 0x0f, CODEC_TYPE_AUDIO,        CODEC_ID_AAC },
-    { 0x10, CODEC_TYPE_VIDEO,      CODEC_ID_MPEG4 },
-    { 0x11, CODEC_TYPE_AUDIO,        CODEC_ID_AAC }, /* LATM syntax */
-    { 0x1b, CODEC_TYPE_VIDEO,       CODEC_ID_H264 },
-    { 0xd1, CODEC_TYPE_VIDEO,      CODEC_ID_DIRAC },
-    { 0xea, CODEC_TYPE_VIDEO,        CODEC_ID_VC1 },
+    { 0x01, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
+    { 0x02, AVMEDIA_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
+    { 0x03, AVMEDIA_TYPE_AUDIO,        CODEC_ID_MP3 },
+    { 0x04, AVMEDIA_TYPE_AUDIO,        CODEC_ID_MP3 },
+    { 0x0f, AVMEDIA_TYPE_AUDIO,        CODEC_ID_AAC },
+    { 0x10, AVMEDIA_TYPE_VIDEO,      CODEC_ID_MPEG4 },
+  //{ 0x11, AVMEDIA_TYPE_AUDIO,        CODEC_ID_AAC }, /* LATM syntax */
+    { 0x1b, AVMEDIA_TYPE_VIDEO,       CODEC_ID_H264 },
+    { 0xd1, AVMEDIA_TYPE_VIDEO,      CODEC_ID_DIRAC },
+    { 0xea, AVMEDIA_TYPE_VIDEO,        CODEC_ID_VC1 },
     { 0 },
 };
 
 static const StreamType HDMV_types[] = {
-    { 0x80, CODEC_TYPE_AUDIO, CODEC_ID_PCM_BLURAY },
-    { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
-    { 0x82, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
-    { 0x83, CODEC_TYPE_AUDIO, CODEC_ID_TRUEHD },
-    { 0x84, CODEC_TYPE_AUDIO, CODEC_ID_EAC3 },
-    { 0x90, CODEC_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
+    { 0x80, AVMEDIA_TYPE_AUDIO, CODEC_ID_PCM_BLURAY },
+    { 0x81, AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3 },
+    { 0x82, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS },
+    { 0x83, AVMEDIA_TYPE_AUDIO, CODEC_ID_TRUEHD },
+    { 0x84, AVMEDIA_TYPE_AUDIO, CODEC_ID_EAC3 },
+    { 0x90, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
     { 0 },
 };
 
 /* ATSC ? */
 static const StreamType MISC_types[] = {
-    { 0x81, CODEC_TYPE_AUDIO,   CODEC_ID_AC3 },
-    { 0x8a, CODEC_TYPE_AUDIO,   CODEC_ID_DTS },
+    { 0x81, AVMEDIA_TYPE_AUDIO,   CODEC_ID_AC3 },
+    { 0x8a, AVMEDIA_TYPE_AUDIO,   CODEC_ID_DTS },
     { 0 },
 };
 
 static const StreamType REGD_types[] = {
-    { MKTAG('d','r','a','c'), CODEC_TYPE_VIDEO, CODEC_ID_DIRAC },
-    { MKTAG('A','C','-','3'), CODEC_TYPE_AUDIO,   CODEC_ID_AC3 },
+    { MKTAG('d','r','a','c'), AVMEDIA_TYPE_VIDEO, CODEC_ID_DIRAC },
+    { MKTAG('A','C','-','3'), AVMEDIA_TYPE_AUDIO,   CODEC_ID_AC3 },
     { 0 },
 };
 
 /* descriptor present */
 static const StreamType DESC_types[] = {
-    { 0x6a, CODEC_TYPE_AUDIO,             CODEC_ID_AC3 }, /* AC-3 descriptor */
-    { 0x7a, CODEC_TYPE_AUDIO,            CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
-    { 0x7b, CODEC_TYPE_AUDIO,             CODEC_ID_DTS },
-    { 0x56, CODEC_TYPE_SUBTITLE, CODEC_ID_DVB_TELETEXT },
-    { 0x59, CODEC_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
+    { 0x6a, AVMEDIA_TYPE_AUDIO,             CODEC_ID_AC3 }, /* AC-3 descriptor */
+    { 0x7a, AVMEDIA_TYPE_AUDIO,            CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
+    { 0x7b, AVMEDIA_TYPE_AUDIO,             CODEC_ID_DTS },
+    { 0x56, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_TELETEXT },
+    { 0x59, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
     { 0 },
 };
 
@@ -554,7 +554,7 @@ static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
 {
     av_set_pts_info(st, 33, 1, 90000);
     st->priv_data = pes;
-    st->codec->codec_type = CODEC_TYPE_DATA;
+    st->codec->codec_type = AVMEDIA_TYPE_DATA;
     st->codec->codec_id   = CODEC_ID_NONE;
     st->need_parsing = AVSTREAM_PARSE_FULL;
     pes->st = st;
@@ -588,12 +588,15 @@ static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
 
             av_set_pts_info(sub_st, 33, 1, 90000);
             sub_st->priv_data = sub_pes;
-            sub_st->codec->codec_type = CODEC_TYPE_AUDIO;
+            sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
             sub_st->codec->codec_id   = CODEC_ID_AC3;
             sub_st->need_parsing = AVSTREAM_PARSE_FULL;
             sub_pes->sub_st = pes->sub_st = sub_st;
         }
     }
+    if (pes->stream_type == 0x11)
+        av_log(pes->stream, AV_LOG_WARNING,
+               "AAC LATM not currently supported, patch welcome\n");
     if (st->codec->codec_id == CODEC_ID_NONE)
         mpegts_find_stream_type(st, pes->stream_type, MISC_types);
 
@@ -804,6 +807,16 @@ static int mpegts_push_data(MpegTSFilter *filter,
                 pes->data_index += buf_size;
             }
             buf_size = 0;
+            /* emit complete packets with known packet size
+             * decreases demuxer delay for infrequent packets like subtitles from
+             * a couple of seconds to milliseconds for properly muxed files.
+             * total_size is the number of bytes following pes_packet_length
+             * in the pes header, i.e. not counting the first 6 bytes */
+            if (pes->total_size < MAX_PES_PAYLOAD &&
+                pes->pes_header_size + pes->data_index == pes->total_size + 6) {
+                ts->stop_parse = 1;
+                new_pes_packet(pes, ts->pkt);
+            }
             break;
         case MPEGTS_SKIP:
             buf_size = 0;
@@ -928,7 +941,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
 
         add_pid_to_pmt(ts, h->id, pid);
 
-        av_program_add_stream_index(ts->stream, h->id, st->index);
+        ff_program_add_stream_index(ts->stream, h->id, st->index);
 
         desc_list_len = get16(&p, p_end) & 0xfff;
         if (desc_list_len < 0)
@@ -960,7 +973,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
                 language[1] = get8(&p, desc_end);
                 language[2] = get8(&p, desc_end);
                 language[3] = 0;
-                av_metadata_set(&st->metadata, "language", language);
+                av_metadata_set2(&st->metadata, "language", language, 0);
                 break;
             case 0x59: /* subtitling descriptor */
                 language[0] = get8(&p, desc_end);
@@ -971,14 +984,14 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
                 comp_page = get16(&p, desc_end);
                 anc_page = get16(&p, desc_end);
                 st->codec->sub_id = (anc_page << 16) | comp_page;
-                av_metadata_set(&st->metadata, "language", language);
+                av_metadata_set2(&st->metadata, "language", language, 0);
                 break;
             case 0x0a: /* ISO 639 language descriptor */
                 language[0] = get8(&p, desc_end);
                 language[1] = get8(&p, desc_end);
                 language[2] = get8(&p, desc_end);
                 language[3] = 0;
-                av_metadata_set(&st->metadata, "language", language);
+                av_metadata_set2(&st->metadata, "language", language, 0);
                 break;
             case 0x05: /* registration descriptor */
                 st->codec->codec_tag = bytestream_get_le32(&p);
@@ -993,7 +1006,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
             p = desc_end;
 
             if (prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
-                av_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
+                ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
                 pes->sub_st->codec->codec_tag = st->codec->codec_tag;
             }
         }
@@ -1106,8 +1119,8 @@ static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
                 if (name) {
                     AVProgram *program = av_new_program(ts->stream, sid);
                     if(program) {
-                        av_metadata_set(&program->metadata, "name", name);
-                        av_metadata_set(&program->metadata, "provider_name", provider_name);
+                        av_metadata_set2(&program->metadata, "name", name, 0);
+                        av_metadata_set2(&program->metadata, "provider_name", provider_name, 0);
                     }
                 }
                 av_free(name);
@@ -1299,7 +1312,7 @@ static int mpegts_probe(AVProbeData *p)
     else                                    return -1;
 #else
     /* only use the extension for safer guess */
-    if (match_ext(p->filename, "ts"))
+    if (av_match_ext(p->filename, "ts"))
         return AVPROBE_SCORE_MAX;
     else
         return 0;
@@ -1394,7 +1407,7 @@ static int mpegts_read_header(AVFormatContext *s,
         if (!st)
             goto fail;
         av_set_pts_info(st, 60, 1, 27000000);
-        st->codec->codec_type = CODEC_TYPE_DATA;
+        st->codec->codec_type = AVMEDIA_TYPE_DATA;
         st->codec->codec_id = CODEC_ID_MPEG2TS;
 
         /* we iterate until we find two PCRs to estimate the bitrate */