split audio chunks in mov demuxer
authorBaptiste Coudurier <baptiste.coudurier@gmail.com>
Sun, 31 May 2009 04:36:26 +0000 (04:36 +0000)
committerBaptiste Coudurier <baptiste.coudurier@gmail.com>
Sun, 31 May 2009 04:36:26 +0000 (04:36 +0000)
Originally committed as revision 19018 to svn://svn.ffmpeg.org/ffmpeg/trunk

libavformat/mov.c

index 95341e6..1c51e8b 100644 (file)
@@ -1403,49 +1403,47 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
                 }
             }
         }
-    } else { /* read whole chunk */
-        unsigned int chunk_samples, chunk_size, chunk_duration;
-        unsigned int frames = 1;
+    } else {
         for (i = 0; i < sc->chunk_count; i++) {
+            unsigned chunk_samples;
+
             current_offset = sc->chunk_offsets[i];
             if (stsc_index + 1 < sc->stsc_count &&
                 i + 1 == sc->stsc_data[stsc_index + 1].first)
                 stsc_index++;
             chunk_samples = sc->stsc_data[stsc_index].count;
-            /* get chunk size, beware of alaw/ulaw/mace */
-            if (sc->samples_per_frame > 0 &&
-                (chunk_samples * sc->bytes_per_frame % sc->samples_per_frame == 0)) {
-                if (sc->samples_per_frame < 160)
-                    chunk_size = chunk_samples * sc->bytes_per_frame / sc->samples_per_frame;
-                else {
-                    chunk_size = sc->bytes_per_frame;
-                    frames = chunk_samples / sc->samples_per_frame;
-                    chunk_samples = sc->samples_per_frame;
-                }
-            } else
-                chunk_size = chunk_samples * sc->sample_size;
-            for (j = 0; j < frames; j++) {
-                av_add_index_entry(st, current_offset, current_dts, chunk_size, 0, AVINDEX_KEYFRAME);
-                /* get chunk duration */
-                chunk_duration = 0;
-                while (chunk_samples > 0) {
-                    if (chunk_samples < sc->stts_data[stts_index].count) {
-                        chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
-                        sc->stts_data[stts_index].count -= chunk_samples;
-                        break;
+
+            if (sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
+                av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
+                return;
+            }
+
+            while (chunk_samples > 0) {
+                unsigned size, samples;
+
+                if (sc->samples_per_frame >= 160) { // gsm
+                    samples = sc->samples_per_frame;
+                    size = sc->bytes_per_frame;
+                } else {
+                    if (sc->samples_per_frame > 1) {
+                        samples = FFMIN((1024 / sc->samples_per_frame)*
+                                        sc->samples_per_frame, chunk_samples);
+                        size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
                     } else {
-                        chunk_duration += sc->stts_data[stts_index].duration * chunk_samples;
-                        chunk_samples -= sc->stts_data[stts_index].count;
-                        if (stts_index + 1 < sc->stts_count)
-                            stts_index++;
+                        samples = FFMIN(1024, chunk_samples);
+                        size = samples * sc->sample_size;
                     }
                 }
+
+                av_add_index_entry(st, current_offset, current_dts, size, 0, AVINDEX_KEYFRAME);
                 dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
                         "size %d, duration %d\n", st->index, i, current_offset, current_dts,
-                        chunk_size, chunk_duration);
-                current_offset += sc->bytes_per_frame;
-                assert(chunk_duration % sc->time_rate == 0);
-                current_dts += chunk_duration / sc->time_rate;
+                        size, samples);
+
+                current_offset += size;
+                assert(samples % sc->time_rate == 0);
+                current_dts += samples / sc->time_rate;
+                chunk_samples -= samples;
             }
         }
     }