]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/movenc.c
use flags field and remove key_frame field
[ffmpeg] / libavformat / movenc.c
index bb5fbe22b5e6ada480b75281d008452793835558..624189a04a37f99e2bffc10422f5dcb4a7154d8a 100644 (file)
@@ -26,7 +26,8 @@
 #include "avio.h"
 #include "isom.h"
 #include "avc.h"
-#include "libavcodec/bitstream.h"
+#include "libavcodec/get_bits.h"
+#include "libavcodec/put_bits.h"
 
 #undef NDEBUG
 #include <assert.h>
 #define MODE_IPOD 0x20
 
 typedef struct MOVIentry {
-    unsigned int flags, size;
+    unsigned int size;
     uint64_t     pos;
     unsigned int samplesInChunk;
-    char         key_frame;
     unsigned int entries;
-    int64_t      cts;
+    int          cts;
     int64_t      dts;
+#define MOV_SYNC_SAMPLE         0x0001
+    uint32_t     flags;
 } MOVIentry;
 
 typedef struct MOVIndex {
@@ -197,7 +199,7 @@ static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack *track)
     entryPos = url_ftell(pb);
     put_be32(pb, track->entry); // entry count
     for (i=0; i<track->entry; i++) {
-        if(track->cluster[i].key_frame == 1) {
+        if (track->cluster[i].flags & MOV_SYNC_SAMPLE) {
             put_be32(pb, i+1);
             index++;
         }
@@ -546,17 +548,65 @@ static int mov_write_avid_tag(ByteIOContext *pb, MOVTrack *track)
     return 0;
 }
 
-static const AVCodecTag codec_3gp_tags[] = {
-    { CODEC_ID_H263,   MKTAG('s','2','6','3') },
+static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
+{
+    int tag = track->enc->codec_tag;
+
+    if (!codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
+        return 0;
+
+    if      (track->enc->codec_id == CODEC_ID_H264)      tag = MKTAG('a','v','c','1');
+    else if (track->enc->codec_id == CODEC_ID_AC3)       tag = MKTAG('a','c','-','3');
+    else if (track->enc->codec_id == CODEC_ID_DIRAC)     tag = MKTAG('d','r','a','c');
+    else if (track->enc->codec_id == CODEC_ID_MOV_TEXT)  tag = MKTAG('t','x','3','g');
+    else if (track->enc->codec_type == CODEC_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
+    else if (track->enc->codec_type == CODEC_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
+
+    return tag;
+}
+
+static const AVCodecTag codec_ipod_tags[] = {
     { CODEC_ID_H264,   MKTAG('a','v','c','1') },
     { CODEC_ID_MPEG4,  MKTAG('m','p','4','v') },
     { CODEC_ID_AAC,    MKTAG('m','p','4','a') },
-    { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
-    { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
+    { CODEC_ID_ALAC,   MKTAG('a','l','a','c') },
+    { CODEC_ID_AC3,    MKTAG('a','c','-','3') },
     { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
+    { CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
     { CODEC_ID_NONE, 0 },
 };
 
+static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
+{
+    int tag = track->enc->codec_tag;
+
+    // keep original tag for subs, ipod supports both formats
+    if (!(track->enc->codec_type == CODEC_TYPE_SUBTITLE &&
+        (tag == MKTAG('t','x','3','g') ||
+         tag == MKTAG('t','e','x','t'))))
+        tag = codec_get_tag(codec_ipod_tags, track->enc->codec_id);
+
+    if (!match_ext(s->filename, "m4a") && !match_ext(s->filename, "m4v"))
+        av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
+               "Quicktime/Ipod might not play the file\n");
+
+    return tag;
+}
+
+static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
+{
+    int tag;
+
+    if (track->enc->height == 480) /* NTSC */
+        if  (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
+        else                                         tag = MKTAG('d','v','c',' ');
+    else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
+    else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
+    else                                             tag = MKTAG('d','v','p','p');
+
+    return tag;
+}
+
 static const struct {
     enum PixelFormat pix_fmt;
     uint32_t tag;
@@ -565,91 +615,93 @@ static const struct {
     { PIX_FMT_YUYV422, MKTAG('y','u','v','s'),  0 },
     { PIX_FMT_UYVY422, MKTAG('2','v','u','y'),  0 },
     { PIX_FMT_BGR555,  MKTAG('r','a','w',' '), 16 },
+    { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
+    { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
+    { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
     { PIX_FMT_RGB24,   MKTAG('r','a','w',' '), 24 },
-    { PIX_FMT_BGR32_1, MKTAG('r','a','w',' '), 32 },
+    { PIX_FMT_BGR24,   MKTAG('2','4','B','G'), 24 },
+    { PIX_FMT_ARGB,    MKTAG('r','a','w',' '), 32 },
+    { PIX_FMT_BGRA,    MKTAG('B','G','R','A'), 32 },
+    { PIX_FMT_RGBA,    MKTAG('R','G','B','A'), 32 },
 };
 
-static const AVCodecTag codec_ipod_tags[] = {
+static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
+{
+    int tag = track->enc->codec_tag;
+    int i;
+
+    for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
+        if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
+            tag = mov_pix_fmt_tags[i].tag;
+            track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
+            break;
+        }
+    }
+
+    return tag;
+}
+
+static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
+{
+    int tag = track->enc->codec_tag;
+
+    if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
+                 (tag == MKTAG('d','v','c','p') ||
+                  track->enc->codec_id == CODEC_ID_RAWVIDEO ||
+                  av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
+        if (track->enc->codec_id == CODEC_ID_DVVIDEO)
+            tag = mov_get_dv_codec_tag(s, track);
+        else if (track->enc->codec_id == CODEC_ID_RAWVIDEO)
+            tag = mov_get_rawvideo_codec_tag(s, track);
+        else if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
+            tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
+            if (!tag) { // if no mac fcc found, try with Microsoft tags
+                tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
+                if (tag)
+                    av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
+                           "the file may be unplayable!\n");
+            }
+        } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
+            tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
+            if (!tag) { // if no mac fcc found, try with Microsoft tags
+                int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
+                if (ms_tag) {
+                    tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
+                    av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
+                           "the file may be unplayable!\n");
+                }
+            }
+        } else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE)
+            tag = codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
+    }
+
+    return tag;
+}
+
+static const AVCodecTag codec_3gp_tags[] = {
+    { CODEC_ID_H263,   MKTAG('s','2','6','3') },
     { CODEC_ID_H264,   MKTAG('a','v','c','1') },
     { CODEC_ID_MPEG4,  MKTAG('m','p','4','v') },
     { CODEC_ID_AAC,    MKTAG('m','p','4','a') },
-    { CODEC_ID_ALAC,   MKTAG('a','l','a','c') },
-    { CODEC_ID_AC3,    MKTAG('a','c','-','3') },
+    { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
+    { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
     { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
-    { CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
     { CODEC_ID_NONE, 0 },
 };
 
 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
 {
     int tag = track->enc->codec_tag;
-    if (track->mode == MODE_MP4 || track->mode == MODE_PSP) {
-        if (!codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
-            return 0;
-        if      (track->enc->codec_id == CODEC_ID_H264)      tag = MKTAG('a','v','c','1');
-        else if (track->enc->codec_id == CODEC_ID_AC3)       tag = MKTAG('a','c','-','3');
-        else if (track->enc->codec_id == CODEC_ID_DIRAC)     tag = MKTAG('d','r','a','c');
-        else if (track->enc->codec_id == CODEC_ID_MOV_TEXT)  tag = MKTAG('t','x','3','g');
-        else if (track->enc->codec_type == CODEC_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
-        else if (track->enc->codec_type == CODEC_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
-    } else if (track->mode == MODE_IPOD) {
-        if (track->enc->codec_type == CODEC_TYPE_SUBTITLE &&
-            (tag == MKTAG('t','x','3','g') ||
-             tag == MKTAG('t','e','x','t')))
-            track->tag = tag; // keep original tag
-        else
-            tag = codec_get_tag(codec_ipod_tags, track->enc->codec_id);
-        if (!match_ext(s->filename, "m4a") && !match_ext(s->filename, "m4v"))
-            av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
-                   "Quicktime/Ipod might not play the file\n");
-    } else if (track->mode & MODE_3GP) {
+
+    if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
+        tag = mp4_get_codec_tag(s, track);
+    else if (track->mode == MODE_IPOD)
+        tag = ipod_get_codec_tag(s, track);
+    else if (track->mode & MODE_3GP)
         tag = codec_get_tag(codec_3gp_tags, track->enc->codec_id);
-    } else if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
-                        (tag == MKTAG('d','v','c','p') ||
-                         track->enc->codec_id == CODEC_ID_RAWVIDEO ||
-                         av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
-        if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
-            if (track->enc->height == 480) /* NTSC */
-                if  (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
-                else                                         tag = MKTAG('d','v','c',' ');
-            else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
-            else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
-            else                                             tag = MKTAG('d','v','p','p');
-        } else if (track->enc->codec_id == CODEC_ID_RAWVIDEO) {
-            int i;
-            for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
-                if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
-                    tag = mov_pix_fmt_tags[i].tag;
-                    track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
-                    break;
-                }
-            }
-            if (!tag) // restore tag
-                tag = track->enc->codec_tag;
-        } else {
-            if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
-                tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
-                if (!tag) { // if no mac fcc found, try with Microsoft tags
-                    tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
-                    if (tag)
-                        av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
-                               "the file may be unplayable!\n");
-                }
-            } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
-                tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
-                if (!tag) { // if no mac fcc found, try with Microsoft tags
-                    int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
-                    if (ms_tag) {
-                        tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
-                        av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
-                               "the file may be unplayable!\n");
-                    }
-                }
-            } else if (track->enc->codec_type == CODEC_TYPE_SUBTITLE) {
-                tag = codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
-            }
-        }
-    }
+    else
+        tag = mov_get_codec_tag(s, track);
+
     return tag;
 }
 
@@ -1707,7 +1759,6 @@ static int mov_write_header(AVFormatContext *s)
                 track->height = track->tag>>24 == 'n' ? 486 : 576;
             }
             track->timescale = st->codec->time_base.den;
-            av_set_pts_info(st, 64, 1, st->codec->time_base.den);
             if (track->mode == MODE_MOV && track->timescale > 100000)
                 av_log(s, AV_LOG_WARNING,
                        "WARNING codec timebase is very high. If duration is too long,\n"
@@ -1715,7 +1766,6 @@ static int mov_write_header(AVFormatContext *s)
                        "or choose different container.\n");
         }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
             track->timescale = st->codec->sample_rate;
-            av_set_pts_info(st, 64, 1, st->codec->sample_rate);
             if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
                 av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
                 goto error;
@@ -1733,10 +1783,11 @@ static int mov_write_header(AVFormatContext *s)
             }
         }else if(st->codec->codec_type == CODEC_TYPE_SUBTITLE){
             track->timescale = st->codec->time_base.den;
-            av_set_pts_info(st, 64, 1, st->codec->time_base.den);
         }
         if (!track->height)
             track->height = st->codec->height;
+
+        av_set_pts_info(st, 64, 1, track->timescale);
     }
 
     mov_write_mdat_tag(pb, mov);
@@ -1798,7 +1849,7 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
     }
 
     if ((enc->codec_id == CODEC_ID_DNXHD ||
-                enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
+         enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
         /* copy frame to create needed atoms */
         trk->vosLen = size;
         trk->vosData = av_malloc(size);
@@ -1827,9 +1878,10 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
     if (pkt->dts != pkt->pts)
         trk->hasBframes = 1;
     trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
-    trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
-    if(trk->cluster[trk->entry].key_frame)
+    if (pkt->flags & PKT_FLAG_KEY) {
+        trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
         trk->hasKeyframes++;
+    }
     trk->entry++;
     trk->sampleCount += samplesInChunk;
     mov->mdat_size += size;