]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/movenc.c
attempt to demux clip wrapped content
[ffmpeg] / libavformat / movenc.c
index fed2a6e3d2c33e36fcd7cb7fb26df0cd3216032f..de9b4a5f2593fb179a594c1da4a42db0063f9f55 100644 (file)
@@ -18,9 +18,9 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include "avformat.h"
-#include "avi.h"
+#include "riff.h"
 #include "avio.h"
-#include "mov.h"
+#include "isom.h"
 
 #undef NDEBUG
 #include <assert.h>
@@ -52,6 +52,7 @@ typedef struct MOVIndex {
     int64_t     trackDuration;
     long        sampleCount;
     long        sampleDuration;
+    long        sampleSize;
     int         hasKeyframes;
     int         hasBframes;
     int         language;
@@ -199,29 +200,17 @@ static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
     return updateSize (pb, pos);
 }
 
-static int mov_write_damr_tag(ByteIOContext *pb)
+static int mov_write_amr_tag(ByteIOContext *pb, MOVTrack *track)
 {
     put_be32(pb, 0x11); /* size */
-    put_tag(pb, "damr");
+    if (track->mode == MODE_MOV) put_tag(pb, "samr");
+    else                         put_tag(pb, "damr");
     put_tag(pb, "FFMP");
-    put_byte(pb, 0);
+    put_byte(pb, 0); /* decoder version */
 
-    //put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
-    //put_be16(pb, 0xa); /* Mode change period (no restriction) */
-    put_be16(pb, 0x81ff); /* Mode set (all modes for AMR_NB) */
-    put_be16(pb, 1); /* Mode change period (no restriction) */
-    return 0x11;
-}
-
-static int mov_write_samr_tag(ByteIOContext *pb)
-{
-    put_be32(pb, 0x11); /* size */
-    put_tag(pb, "samr");
-    put_tag(pb, "FFMP");
-    put_byte(pb, 1);
-
-    put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
-    put_be16(pb, 0x5); /* Mode change period (no restriction) */
+    put_be16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
+    put_byte(pb, 0x00); /* Mode change period (no restriction) */
+    put_byte(pb, 0x01); /* Frames per sample */
     return 0x11;
 }
 
@@ -321,7 +310,7 @@ static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
                track->enc->codec_id == CODEC_ID_PCM_S32LE) {
         mov_write_enda_tag(pb);
     } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
-        mov_write_samr_tag(pb);
+        mov_write_amr_tag(pb, track);
     }
 
     put_be32(pb, 8);     /* size */
@@ -352,9 +341,11 @@ static const CodecTag codec_movaudio_tags[] = {
 static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
 {
     offset_t pos = url_ftell(pb);
+    int vbr=  track->enc->codec_id == CODEC_ID_AAC ||
+              track->enc->codec_id == CODEC_ID_MP3 ||
+              track->enc->codec_id == CODEC_ID_AMR_NB;
     int version = track->mode == MODE_MOV &&
-        (track->enc->codec_id == CODEC_ID_AAC ||
-         track->enc->codec_id == CODEC_ID_MP3 ||
+        (vbr ||
          track->enc->codec_id == CODEC_ID_PCM_S32LE ||
          track->enc->codec_id == CODEC_ID_PCM_S24LE);
 
@@ -374,9 +365,7 @@ static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
                  to be a good way to get number of bits of audio */
     put_be16(pb, 0x10); /* Reserved */
 
-    if(track->enc->codec_id == CODEC_ID_AAC ||
-       track->enc->codec_id == CODEC_ID_MP3 ||
-       track->enc->codec_id == CODEC_ID_AMR_NB) {
+    if(vbr) {
         put_be16(pb, 0xfffe); /* compression ID (vbr)*/
     } else {
         put_be16(pb, 0); /* compression ID (= 0) */
@@ -385,15 +374,12 @@ static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
     put_be16(pb, track->timescale); /* Time scale */
     put_be16(pb, 0); /* Reserved */
 
-    if(version == 1) {
-        /* SoundDescription V1 extended info */
+    if(version == 1) { /* SoundDescription V1 extended info */
+        if (vbr)
+            track->sampleSize = 0;
         put_be32(pb, track->enc->frame_size); /* Samples per packet */
-        /* Parameters tested on quicktime 6.5, 7 */
-        put_be32(pb, 1); /* Bytes per packet */
-        /* FIXME not correct */
-        /* 8 is the min value needed for in32 to work with quicktime 6.5 */
-        /* Value ignored by other codecs currently supported (others might need it) */
-        put_be32(pb, 8); /* Bytes per frame */
+        put_be32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
+        put_be32(pb, track->sampleSize); /* Bytes per frame */
         put_be32(pb, 2); /* Bytes per sample */
     }
 
@@ -406,7 +392,7 @@ static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
     else if(track->enc->codec_id == CODEC_ID_AAC)
         mov_write_esds_tag(pb, track);
     else if(track->enc->codec_id == CODEC_ID_AMR_NB)
-        mov_write_damr_tag(pb);
+        mov_write_amr_tag(pb, track);
 
     return updateSize (pb, pos);
 }
@@ -1477,6 +1463,7 @@ static int mov_write_header(AVFormatContext *s)
         }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
             track->tag = mov_find_audio_codec_tag(s, track);
             av_set_pts_info(st, 64, 1, st->codec->sample_rate);
+            track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
         }
         track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
         track->mode = mov->mode;
@@ -1503,42 +1490,24 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
     if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
     if (!size) return 0; /* Discard 0 sized packets */
 
-    if (enc->codec_type == CODEC_TYPE_AUDIO) {
-        switch (enc->codec_id) {
-        case CODEC_ID_AMR_NB:
-            { /* We must find out how many AMR blocks there are in one packet */
-                static uint16_t packed_size[16] =
-                    {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
-                int len = 0;
-
-                while (len < size && samplesInChunk < 100) {
-                    len += packed_size[(pkt->data[len] >> 3) & 0x0F];
-                    samplesInChunk++;
-                }
-            }
-            break;
-        case CODEC_ID_PCM_MULAW:
-        case CODEC_ID_PCM_ALAW:
-            samplesInChunk = size/enc->channels;
-            break;
-        case CODEC_ID_PCM_S16BE:
-        case CODEC_ID_PCM_S16LE:
-            samplesInChunk = size/(2*enc->channels);
-            break;
-        case CODEC_ID_PCM_S24BE:
-        case CODEC_ID_PCM_S24LE:
-            samplesInChunk = size/(3*enc->channels);
-            break;
-        case CODEC_ID_PCM_S32BE:
-        case CODEC_ID_PCM_S32LE:
-            samplesInChunk = size/(4*enc->channels);
-            break;
-        default:
-            samplesInChunk = 1;
+    if (enc->codec_id == CODEC_ID_AMR_NB) {
+        /* We must find out how many AMR blocks there are in one packet */
+        static uint16_t packed_size[16] =
+            {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
+        int len = 0;
+
+        while (len < size && samplesInChunk < 100) {
+            len += packed_size[(pkt->data[len] >> 3) & 0x0F];
+            samplesInChunk++;
         }
-    } else {
+        if(samplesInChunk > 1){
+            av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, inplement a AVParser for it\n");
+            return -1;
+        }
+    } else if (trk->sampleSize)
+        samplesInChunk = size/trk->sampleSize;
+    else
         samplesInChunk = 1;
-    }
 
     /* copy extradata if it exists */
     if (trk->vosLen == 0 && enc->extradata_size > 0) {
@@ -1619,7 +1588,8 @@ static int mov_write_trailer(AVFormatContext *s)
     return res;
 }
 
-static AVOutputFormat mov_oformat = {
+#ifdef CONFIG_MOV_MUXER
+AVOutputFormat mov_muxer = {
     "mov",
     "mov format",
     NULL,
@@ -1632,8 +1602,9 @@ static AVOutputFormat mov_oformat = {
     mov_write_trailer,
     .flags = AVFMT_GLOBALHEADER,
 };
-
-static AVOutputFormat _3gp_oformat = {
+#endif
+#ifdef CONFIG_TGP_MUXER
+AVOutputFormat tgp_muxer = {
     "3gp",
     "3gp format",
     NULL,
@@ -1646,8 +1617,9 @@ static AVOutputFormat _3gp_oformat = {
     mov_write_trailer,
     .flags = AVFMT_GLOBALHEADER,
 };
-
-static AVOutputFormat mp4_oformat = {
+#endif
+#ifdef CONFIG_MP4_MUXER
+AVOutputFormat mp4_muxer = {
     "mp4",
     "mp4 format",
     "application/mp4",
@@ -1660,8 +1632,9 @@ static AVOutputFormat mp4_oformat = {
     mov_write_trailer,
     .flags = AVFMT_GLOBALHEADER,
 };
-
-static AVOutputFormat psp_oformat = {
+#endif
+#ifdef CONFIG_PSP_MUXER
+AVOutputFormat psp_muxer = {
     "psp",
     "psp mp4 format",
     NULL,
@@ -1674,8 +1647,9 @@ static AVOutputFormat psp_oformat = {
     mov_write_trailer,
     .flags = AVFMT_GLOBALHEADER,
 };
-
-static AVOutputFormat _3g2_oformat = {
+#endif
+#ifdef CONFIG_TG2_MUXER
+AVOutputFormat tg2_muxer = {
     "3g2",
     "3gp2 format",
     NULL,
@@ -1688,13 +1662,4 @@ static AVOutputFormat _3g2_oformat = {
     mov_write_trailer,
     .flags = AVFMT_GLOBALHEADER,
 };
-
-int movenc_init(void)
-{
-    av_register_output_format(&mov_oformat);
-    av_register_output_format(&_3gp_oformat);
-    av_register_output_format(&mp4_oformat);
-    av_register_output_format(&psp_oformat);
-    av_register_output_format(&_3g2_oformat);
-    return 0;
-}
+#endif