Merge commit '21732063a346475eb22c332b27e8216b79f9ad4a'
authorMichael Niedermayer <michaelni@gmx.at>
Mon, 8 Jul 2013 02:08:07 +0000 (04:08 +0200)
committerMichael Niedermayer <michaelni@gmx.at>
Mon, 8 Jul 2013 02:08:07 +0000 (04:08 +0200)
* commit '21732063a346475eb22c332b27e8216b79f9ad4a':
  movenc: K&R formatting cosmetics

Conflicts:
libavformat/movenc.c

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

@@@ -108,19 -85,18 +108,19 @@@ static int is_co64_required(const MOVTr
  static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
  {
      int i;
 -    int mode64 = 0; //   use 32 bit size variant if possible
 +    int mode64 = is_co64_required(track); // use 32 bit size variant if possible
      int64_t pos = avio_tell(pb);
      avio_wb32(pb, 0); /* size */
 -    if (pos > UINT32_MAX) {
 -        mode64 = 1;
 +    if (mode64)
          ffio_wfourcc(pb, "co64");
 -    else
 +    else
          ffio_wfourcc(pb, "stco");
      avio_wb32(pb, 0); /* version & flags */
 -    avio_wb32(pb, track->entry); /* entry count */
 +    avio_wb32(pb, track->chunkCount); /* entry count */
-     for (i=0; i<track->entry; i++) {
-         if(!track->cluster[i].chunkNum)
+     for (i = 0; i < track->entry; i++) {
++        if (!track->cluster[i].chunkNum)
 +            continue;
-         if(mode64 == 1)
+         if (mode64 == 1)
              avio_wb64(pb, track->cluster[i].pos + track->data_offset);
          else
              avio_wb32(pb, track->cluster[i].pos + track->data_offset);
@@@ -177,11 -151,10 +175,10 @@@ static int mov_write_stsc_tag(AVIOConte
      ffio_wfourcc(pb, "stsc");
      avio_wb32(pb, 0); // version & flags
      entryPos = avio_tell(pb);
 -    avio_wb32(pb, track->entry); // entry count
 +    avio_wb32(pb, track->chunkCount); // entry count
-     for (i=0; i<track->entry; i++) {
-         if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum)
-         {
+     for (i = 0; i < track->entry; i++) {
 -        if (oldval != track->cluster[i].samples_in_chunk) {
 -            avio_wb32(pb, i + 1); // first chunk
++        if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum) {
 +            avio_wb32(pb, track->cluster[i].chunkNum); // first chunk
              avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
              avio_wb32(pb, 0x1); // sample description index
              oldval = track->cluster[i].samples_in_chunk;
@@@ -708,35 -613,21 +705,35 @@@ static int mov_write_audio_tag(AVIOCont
          avio_wb16(pb, 0); /* Reserved */
      }
  
-     if(version == 1) { /* SoundDescription V1 extended info */
++    if (version == 1) { /* SoundDescription V1 extended info */
 +        if (mov_pcm_le_gt16(track->enc->codec_id) ||
 +            mov_pcm_be_gt16(track->enc->codec_id))
 +            avio_wb32(pb, 1); /*  must be 1 for  uncompressed formats */
 +        else
 +            avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
 +        avio_wb32(pb, track->sample_size / track->enc->channels); /* Bytes per packet */
 +        avio_wb32(pb, track->sample_size); /* Bytes per frame */
 +        avio_wb32(pb, 2); /* Bytes per sample */
 +    }
 +
-     if(track->mode == MODE_MOV &&
-        (track->enc->codec_id == AV_CODEC_ID_AAC ||
-         track->enc->codec_id == AV_CODEC_ID_AC3 ||
-         track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
-         track->enc->codec_id == AV_CODEC_ID_ALAC ||
-         track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
-         track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
-         track->enc->codec_id == AV_CODEC_ID_QDM2 ||
-         (mov_pcm_le_gt16(track->enc->codec_id) && version==1) ||
-         (mov_pcm_be_gt16(track->enc->codec_id) && version==1)))
+     if (track->mode == MODE_MOV &&
+         (track->enc->codec_id == AV_CODEC_ID_AAC           ||
+          track->enc->codec_id == AV_CODEC_ID_AC3           ||
+          track->enc->codec_id == AV_CODEC_ID_AMR_NB        ||
+          track->enc->codec_id == AV_CODEC_ID_ALAC          ||
+          track->enc->codec_id == AV_CODEC_ID_ADPCM_MS      ||
 -         track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV))
++         track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
++         track->enc->codec_id == AV_CODEC_ID_QDM2          ||
++         (mov_pcm_le_gt16(track->enc->codec_id) && version==1) ||
++         (mov_pcm_be_gt16(track->enc->codec_id) && version==1)))
          mov_write_wave_tag(pb, track);
-     else if(track->tag == MKTAG('m','p','4','a'))
+     else if (track->tag == MKTAG('m','p','4','a'))
          mov_write_esds_tag(pb, track);
-     else if(track->enc->codec_id == AV_CODEC_ID_AMR_NB)
+     else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB)
          mov_write_amr_tag(pb, track);
-     else if(track->enc->codec_id == AV_CODEC_ID_AC3)
+     else if (track->enc->codec_id == AV_CODEC_ID_AC3)
          mov_write_ac3_tag(pb, track);
-     else if(track->enc->codec_id == AV_CODEC_ID_ALAC)
+     else if (track->enc->codec_id == AV_CODEC_ID_ALAC)
          mov_write_extradata_tag(pb, track);
      else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
          mov_write_wfex_tag(pb, track);
@@@ -869,20 -773,20 +866,20 @@@ static int mov_get_dv_codec_tag(AVForma
  {
      int tag;
  
 -    if (track->enc->width == 720) /* SD */
 -        if (track->enc->height == 480) /* NTSC */
 +    if (track->enc->width == 720) /* SD */
 +        if (track->enc->height == 480) /* NTSC */
              if  (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
-             else                                         tag = MKTAG('d','v','c',' ');
+             else                                            tag = MKTAG('d','v','c',' ');
 -        else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
 +       }else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
          else if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
-         else                                             tag = MKTAG('d','v','p','p');
+         else                                                tag = MKTAG('d','v','p','p');
 -    else if (track->enc->height == 720) /* HD 720 line */
 +    } else if (track->enc->height == 720) { /* HD 720 line */
-         if  (track->enc->time_base.den == 50)            tag = MKTAG('d','v','h','q');
-         else                                             tag = MKTAG('d','v','h','p');
+         if  (track->enc->time_base.den == 50)               tag = MKTAG('d','v','h','q');
+         else                                                tag = MKTAG('d','v','h','p');
 -    else if (track->enc->height == 1080) /* HD 1080 line */
 +    } else if (track->enc->height == 1080) { /* HD 1080 line */
-         if  (track->enc->time_base.den == 25)            tag = MKTAG('d','v','h','5');
-         else                                             tag = MKTAG('d','v','h','6');
+         if  (track->enc->time_base.den == 25)               tag = MKTAG('d','v','h','5');
+         else                                                tag = MKTAG('d','v','h','6');
 -    else {
 +    else {
          av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
          return 0;
      }
@@@ -1114,21 -1006,21 +1111,21 @@@ static int mov_write_video_tag(AVIOCont
      else
          avio_wb16(pb, 0x18); /* Reserved */
      avio_wb16(pb, 0xffff); /* Reserved */
-     if(track->tag == MKTAG('m','p','4','v'))
+     if (track->tag == MKTAG('m','p','4','v'))
          mov_write_esds_tag(pb, track);
-     else if(track->enc->codec_id == AV_CODEC_ID_H263)
+     else if (track->enc->codec_id == AV_CODEC_ID_H263)
          mov_write_d263_tag(pb);
-     else if(track->enc->codec_id == AV_CODEC_ID_AVUI ||
 -    else if (track->enc->codec_id == AV_CODEC_ID_SVQ3)
 -        mov_write_svq3_tag(pb);
 -    else if (track->enc->codec_id == AV_CODEC_ID_DNXHD)
++    else if (track->enc->codec_id == AV_CODEC_ID_AVUI ||
 +            track->enc->codec_id == AV_CODEC_ID_SVQ3) {
 +        mov_write_extradata_tag(pb, track);
 +        avio_wb32(pb, 0);
-     } else if(track->enc->codec_id == AV_CODEC_ID_DNXHD)
++    } else if (track->enc->codec_id == AV_CODEC_ID_DNXHD)
          mov_write_avid_tag(pb, track);
-     else if(track->enc->codec_id == AV_CODEC_ID_H264) {
+     else if (track->enc->codec_id == AV_CODEC_ID_H264) {
          mov_write_avcc_tag(pb, track);
-         if(track->mode == MODE_IPOD)
+         if (track->mode == MODE_IPOD)
              mov_write_uuid_tag_ipod(pb);
 -    } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
 -        mov_write_fiel_tag(pb, track);
 -    else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
 +    } else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
          mov_write_dvc1_tag(pb, track);
      else if (track->vos_len > 0)
          mov_write_glbl_tag(pb, track);
@@@ -1441,29 -1247,17 +1438,29 @@@ static int mov_write_hdlr_tag(AVIOConte
          hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
          if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
              hdlr_type = "vide";
-             descr = "VideoHandler";
+             descr     = "VideoHandler";
          } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
              hdlr_type = "soun";
-             descr = "SoundHandler";
+             descr     = "SoundHandler";
          } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
 +            if (track->tag == MKTAG('c','6','0','8')) {
 +                hdlr_type = "clcp";
 +                descr = "ClosedCaptionHandler";
 +            } else {
              if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
              else                                      hdlr_type = "text";
              descr = "SubtitleHandler";
 +            }
 +        } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
 +            hdlr_type = "tmcd";
 +            descr = "TimeCodeHandler";
          } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
              hdlr_type = "hint";
-             descr = "HintHandler";
+             descr     = "HintHandler";
 +        } else {
-             hdlr = "dhlr";
++            hdlr      = "dhlr";
 +            hdlr_type = "url ";
-             descr = "DataHandler";
++            descr     = "DataHandler";
          }
      }
  
@@@ -1630,23 -1400,20 +1627,23 @@@ static int mov_write_tkhd_tag(AVIOConte
      avio_wb16(pb, 0); /* reserved */
  
      /* Matrix structure */
 -    avio_wb32(pb, 0x00010000); /* reserved */
 -    avio_wb32(pb, 0x0); /* reserved */
 -    avio_wb32(pb, 0x0); /* reserved */
 -    avio_wb32(pb, 0x0); /* reserved */
 -    avio_wb32(pb, 0x00010000); /* reserved */
 -    avio_wb32(pb, 0x0); /* reserved */
 -    avio_wb32(pb, 0x0); /* reserved */
 -    avio_wb32(pb, 0x0); /* reserved */
 -    avio_wb32(pb, 0x40000000); /* reserved */
 -
 +    if (st && st->metadata) {
 +        AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
 +        rotation = (rot && rot->value) ? atoi(rot->value) : 0;
 +    }
 +    if (rotation == 90) {
 +        write_matrix(pb,  0,  1, -1,  0, track->enc->height, 0);
 +    } else if (rotation == 180) {
 +        write_matrix(pb, -1,  0,  0, -1, track->enc->width, track->enc->height);
 +    } else if (rotation == 270) {
 +        write_matrix(pb,  0, -1,  1,  0, 0, track->enc->width);
 +    } else {
 +        write_matrix(pb,  1,  0,  0,  1, 0, 0);
 +    }
      /* Track width and height, for visual only */
-     if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
-               track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
-         if(track->mode == MODE_MOV) {
+     if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
+                track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
+         if (track->mode == MODE_MOV) {
              avio_wb32(pb, track->enc->width << 16);
              avio_wb32(pb, track->height << 16);
          } else {
@@@ -1816,9 -1574,9 +1811,9 @@@ static int mov_write_trak_tag(AVIOConte
          mov_write_udta_sdp(pb, track);
      if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
          double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
 -        if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
 +        if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio)
              mov_write_tapt_tag(pb, track);
-     };
+     }
      return update_size(pb, pos);
  }
  
@@@ -2217,18 -1936,15 +2216,18 @@@ static int mov_write_udta_tag(AVIOConte
          mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
          mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
      } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
-         mov_write_string_metadata(s, pb_buf, "\251ART", "artist"     , 0);
-         mov_write_string_metadata(s, pb_buf, "\251nam", "title"      , 0);
-         mov_write_string_metadata(s, pb_buf, "\251aut", "author"     , 0);
-         mov_write_string_metadata(s, pb_buf, "\251alb", "album"      , 0);
-         mov_write_string_metadata(s, pb_buf, "\251day", "date"       , 0);
-         mov_write_string_metadata(s, pb_buf, "\251swr", "encoder"    , 0);
+         mov_write_string_metadata(s, pb_buf, "\251ART", "artist",      0);
+         mov_write_string_metadata(s, pb_buf, "\251nam", "title",       0);
+         mov_write_string_metadata(s, pb_buf, "\251aut", "author",      0);
+         mov_write_string_metadata(s, pb_buf, "\251alb", "album",       0);
+         mov_write_string_metadata(s, pb_buf, "\251day", "date",        0);
+         mov_write_string_metadata(s, pb_buf, "\251swr", "encoder",     0);
 +        // currently ignored by mov.c
-         mov_write_string_metadata(s, pb_buf, "\251des", "comment"    , 0);
+         mov_write_string_metadata(s, pb_buf, "\251des", "comment",     0);
 +        // add support for libquicktime, this atom is also actually read by mov.c
-         mov_write_string_metadata(s, pb_buf, "\251cmt", "comment"    , 0);
-         mov_write_string_metadata(s, pb_buf, "\251gen", "genre"      , 0);
-         mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright"  , 0);
++        mov_write_string_metadata(s, pb_buf, "\251cmt", "comment",     0);
+         mov_write_string_metadata(s, pb_buf, "\251gen", "genre",       0);
+         mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright",   0);
      } else {
          /* iTunes meta data */
          mov_write_meta_tag(pb_buf, mov, s);
@@@ -2297,29 -2013,6 +2296,29 @@@ static int mov_write_uuidusmt_tag(AVIOC
      return 0;
  }
  
-     MOVIentry *chunk= &trk->cluster[0];
 +static void build_chunks(MOVTrack *trk)
 +{
 +    int i;
-     chunk->chunkNum= 1;
++    MOVIentry *chunk = &trk->cluster[0];
 +    uint64_t chunkSize = chunk->size;
-     trk->chunkCount= 1;
-     for(i=1; i<trk->entry; i++){
-         if(chunk->pos + chunkSize == trk->cluster[i].pos &&
++    chunk->chunkNum = 1;
 +    if (trk->chunkCount)
 +        return;
-         }else{
++    trk->chunkCount = 1;
++    for (i = 1; i<trk->entry; i++){
++        if (chunk->pos + chunkSize == trk->cluster[i].pos &&
 +            chunkSize + trk->cluster[i].size < (1<<20)){
 +            chunkSize             += trk->cluster[i].size;
 +            chunk->samples_in_chunk += trk->cluster[i].entries;
++        } else {
 +            trk->cluster[i].chunkNum = chunk->chunkNum+1;
 +            chunk=&trk->cluster[i];
 +            chunkSize = chunk->size;
 +            trk->chunkCount++;
 +        }
 +    }
 +}
 +
  static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
                                AVFormatContext *s)
  {
          if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
              continue;
  
-         mov->tracks[i].time = mov->time;
-         mov->tracks[i].track_id = i+1;
+         mov->tracks[i].time     = mov->time;
+         mov->tracks[i].track_id = i + 1;
 +
 +        if (mov->tracks[i].entry)
 +            build_chunks(&mov->tracks[i]);
      }
  
      if (mov->chapter_track)
      mov_write_mvhd_tag(pb, mov);
      if (mov->mode != MODE_MOV && !mov->iods_skip)
          mov_write_iods_tag(pb, mov);
-     for (i=0; i<mov->nb_streams; i++) {
+     for (i = 0; i < mov->nb_streams; i++) {
          if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
-             if(i < s->nb_streams){
++            if (i < s->nb_streams){
 +                int codec_type= s->streams[i]->codec->codec_type;
-                 if(codec_type==AVMEDIA_TYPE_AUDIO || codec_type==AVMEDIA_TYPE_SUBTITLE){
++                if (codec_type==AVMEDIA_TYPE_AUDIO || codec_type==AVMEDIA_TYPE_SUBTITLE){
 +                    mov->tracks[i].secondary= not_first[codec_type];
 +                    not_first[codec_type]= 1;
 +                }
 +            }
              mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
          }
      }
@@@ -2646,10 -2320,11 +2645,10 @@@ static int mov_write_tfrf_tag(AVIOConte
          avio_wb64(pb, track->frag_info[index].duration);
      }
      if (n < mov->ism_lookahead) {
-         int free_size = 16*(mov->ism_lookahead - n);
+         int free_size = 16 * (mov->ism_lookahead - n);
          avio_wb32(pb, free_size);
          ffio_wfourcc(pb, "free");
 -        for (i = 0; i < free_size - 8; i++)
 -            avio_w8(pb, 0);
 +        ffio_fill(pb, 0, free_size - 8);
      }
  
      return 0;
@@@ -3231,12 -2882,11 +3230,12 @@@ int ff_mov_write_packet(AVFormatContex
          trk->cluster_capacity = new_capacity;
      }
  
-     trk->cluster[trk->entry].pos = avio_tell(pb) - size;
+     trk->cluster[trk->entry].pos              = avio_tell(pb) - size;
      trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
-     trk->cluster[trk->entry].chunkNum = 0;
-     trk->cluster[trk->entry].size = size;
-     trk->cluster[trk->entry].entries = samples_in_chunk;
-     trk->cluster[trk->entry].dts = pkt->dts;
++    trk->cluster[trk->entry].chunkNum         = 0;
+     trk->cluster[trk->entry].size             = size;
+     trk->cluster[trk->entry].entries          = samples_in_chunk;
+     trk->cluster[trk->entry].dts              = pkt->dts;
      if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
          /* First packet of a new fragment. We already wrote the duration
           * of the last packet of the previous fragment based on track_duration,
@@@ -3295,9 -2945,10 +3294,10 @@@ static int mov_write_single_packet(AVFo
          int64_t frag_duration = 0;
          int size = pkt->size;
  
-         if (!pkt->size) return 0; /* Discard 0 sized packets */
+         if (!pkt->size)
+             return 0;             /* Discard 0 sized packets */
  
 -        if (trk->entry)
 +        if (trk->entry && pkt->stream_index < s->nb_streams)
              frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
                                           s->streams[pkt->stream_index]->time_base,
                                           AV_TIME_BASE_Q);
@@@ -3582,33 -3063,7 +3582,33 @@@ static int mov_write_header(AVFormatCon
          }
      }
  
-     mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks));
 +    if (mov->mode == MODE_MOV) {
 +        tmcd_track = mov->nb_streams;
 +
 +        /* +1 tmcd track for each video stream with a timecode */
 +        for (i = 0; i < s->nb_streams; i++) {
 +            AVStream *st = s->streams[i];
 +            if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
 +                (global_tcr || av_dict_get(st->metadata, "timecode", NULL, 0)))
 +                mov->nb_meta_tmcd++;
 +        }
 +
 +        /* check if there is already a tmcd track to remux */
 +        if (mov->nb_meta_tmcd) {
 +            for (i = 0; i < s->nb_streams; i++) {
 +                AVStream *st = s->streams[i];
 +                if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
 +                    av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
 +                           "so timecode metadata are now ignored\n");
 +                    mov->nb_meta_tmcd = 0;
 +                }
 +            }
 +        }
 +
 +        mov->nb_streams += mov->nb_meta_tmcd;
 +    }
 +
+     mov->tracks = av_mallocz(mov->nb_streams * sizeof(*mov->tracks));
      if (!mov->tracks)
          return AVERROR(ENOMEM);
  
                      av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
                      goto error;
                  }
-                 track->height = track->tag>>24 == 'n' ? 486 : 576;
+                 track->height = track->tag >> 24 == 'n' ? 486 : 576;
              }
 -            track->timescale = st->codec->time_base.den;
 +            if (mov->video_track_timescale) {
 +                track->timescale = mov->video_track_timescale;
 +            } else {
 +                track->timescale = st->codec->time_base.den;
 +                while(track->timescale < 10000)
 +                    track->timescale *= 2;
 +            }
              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"
                         "file may not be playable by quicktime. Specify a shorter timebase\n"
                         "or choose different container.\n");
-         }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
+         } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
              track->timescale = st->codec->sample_rate;
-             if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
 -            /* set sample_size for PCM and ADPCM */
 -            if (av_get_bits_per_sample(st->codec->codec_id) ||
 -                st->codec->codec_id == AV_CODEC_ID_ILBC) {
++            if (!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
 +                av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
 +                track->audio_vbr = 1;
-             }else if(st->codec->codec_id == AV_CODEC_ID_ADPCM_MS ||
++            }else if (st->codec->codec_id == AV_CODEC_ID_ADPCM_MS ||
 +                     st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
 +                     st->codec->codec_id == AV_CODEC_ID_ILBC){
                  if (!st->codec->block_align) {
 -                    av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set\n", i);
 +                    av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
                      goto error;
                  }
                  track->sample_size = st->codec->block_align;
-             }else if(st->codec->frame_size > 1){ /* assume compressed audio */
 -            }
 -            /* set audio_vbr for compressed audio */
 -            if (av_get_bits_per_sample(st->codec->codec_id) < 8) {
++            }else if (st->codec->frame_size > 1){ /* assume compressed audio */
                  track->audio_vbr = 1;
 +            }else{
 +                track->sample_size = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
              }
              if (track->mode != MODE_MOV &&
                  track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
                         i, track->enc->sample_rate);
                  goto error;
              }
-         }else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
+         } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
              track->timescale = st->codec->time_base.den;
 +        }else{
 +            track->timescale = MOV_TIMESCALE;
          }
          if (!track->height)
              track->height = st->codec->height;
                        FF_MOV_FLAG_FRAGMENT;
      }
  
-     if(mov->reserved_moov_size){
++    if (mov->reserved_moov_size){
 +        mov->reserved_moov_pos= avio_tell(pb);
 +        if (mov->reserved_moov_size > 0)
 +            avio_skip(pb, mov->reserved_moov_size);
 +    }
 +
      if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
          mov_write_mdat_tag(pb, mov);
  
@@@ -3905,31 -3216,9 +3905,31 @@@ static int mov_write_trailer(AVFormatCo
              ffio_wfourcc(pb, "mdat");
              avio_wb64(pb, mov->mdat_size + 16);
          }
 -        avio_seek(pb, moov_pos, SEEK_SET);
 -
 -        mov_write_moov_tag(pb, mov, s);
 +        avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
 +
 +        if (mov->reserved_moov_size == -1) {
 +            av_log(s, AV_LOG_INFO, "Starting second pass: moving header on top of the file\n");
 +            res = shift_data(s);
 +            if (res == 0) {
 +                avio_seek(s->pb, mov->reserved_moov_pos, SEEK_SET);
 +                mov_write_moov_tag(pb, mov, s);
 +            }
 +        } else if (mov->reserved_moov_size > 0) {
 +            int64_t size;
 +            mov_write_moov_tag(pb, mov, s);
 +            size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
-             if(size < 8){
++            if (size < 8){
 +                av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
 +                return -1;
 +            }
 +            avio_wb32(pb, size);
 +            ffio_wfourcc(pb, "free");
-             for(i=0; i<size; i++)
++            for (i = 0; i < size; i++)
 +                avio_w8(pb, 0);
 +            avio_seek(pb, moov_pos, SEEK_SET);
 +        } else {
 +            mov_write_moov_tag(pb, mov, s);
 +        }
      } else {
          mov_flush_fragment(s);
          mov_write_mfra_tag(pb, mov);
      if (mov->chapter_track)
          av_freep(&mov->tracks[mov->chapter_track].enc);
  
-     for (i=0; i<mov->nb_streams; i++) {
+     for (i = 0; i < mov->nb_streams; i++) {
          if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
              ff_mov_close_hinting(&mov->tracks[i]);
 +        else if (mov->tracks[i].tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
 +            av_freep(&mov->tracks[i].enc);
          if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
              mov->tracks[i].vc1_info.struct_offset && s->pb->seekable) {
              int64_t off = avio_tell(pb);
Simple merge