+static guint64
+prefill_get_block_index (GstQTMux * qtmux, GstQTPad * qpad)
+{
+ switch (qpad->fourcc) {
+ case FOURCC_apch:
+ case FOURCC_apcn:
+ case FOURCC_apcs:
+ case FOURCC_apco:
+ case FOURCC_ap4h:
+ case FOURCC_ap4x:
+ case FOURCC_c608:
+ case FOURCC_c708:
+ return qpad->sample_offset;
+ case FOURCC_sowt:
+ case FOURCC_twos:
+ return gst_util_uint64_scale_ceil (qpad->sample_offset,
+ qpad->expected_sample_duration_n,
+ qpad->expected_sample_duration_d *
+ atom_trak_get_timescale (qpad->trak));
+ default:
+ return -1;
+ }
+}
+
+static guint
+prefill_get_sample_size (GstQTMux * qtmux, GstQTPad * qpad)
+{
+ switch (qpad->fourcc) {
+ case FOURCC_apch:
+ if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 480) {
+ return 300000;
+ } else if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 576) {
+ return 350000;
+ } else if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 720) {
+ return 525000;
+ } else if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 1080) {
+ return 1050000;
+ } else {
+ return 4150000;
+ }
+ break;
+ case FOURCC_apcn:
+ if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 480) {
+ return 200000;
+ } else if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 576) {
+ return 250000;
+ } else if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 720) {
+ return 350000;
+ } else if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 1080) {
+ return 700000;
+ } else {
+ return 2800000;
+ }
+ break;
+ case FOURCC_apcs:
+ if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 480) {
+ return 150000;
+ } else if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 576) {
+ return 200000;
+ } else if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 720) {
+ return 250000;
+ } else if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 1080) {
+ return 500000;
+ } else {
+ return 2800000;
+ }
+ break;
+ case FOURCC_apco:
+ if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 480) {
+ return 80000;
+ } else if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 576) {
+ return 100000;
+ } else if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 720) {
+ return 150000;
+ } else if (((SampleTableEntryMP4V *) qpad->trak_ste)->height <= 1080) {
+ return 250000;
+ } else {
+ return 900000;
+ }
+ break;
+ case FOURCC_c608:
+ /* We always write both cdat and cdt2 atom in prefill mode */
+ return 20;
+ case FOURCC_c708:
+ /* We're cheating a bit by always allocating 256 bytes plus 8 bytes for the atom header
+ * even if we use less */
+ return 256 + 8;
+ case FOURCC_sowt:
+ case FOURCC_twos:{
+ guint64 block_idx;
+ guint64 next_sample_offset;
+
+ block_idx = prefill_get_block_index (qtmux, qpad);
+ next_sample_offset =
+ gst_util_uint64_scale (block_idx + 1,
+ qpad->expected_sample_duration_d *
+ atom_trak_get_timescale (qpad->trak),
+ qpad->expected_sample_duration_n);
+
+ return (next_sample_offset - qpad->sample_offset) * qpad->sample_size;
+ }
+ case FOURCC_ap4h:
+ case FOURCC_ap4x:
+ default:
+ GST_ERROR_OBJECT (qtmux, "unsupported codec for pre-filling");
+ return -1;
+ }
+
+ return -1;
+}
+
+static GstClockTime
+prefill_get_next_timestamp (GstQTMux * qtmux, GstQTPad * qpad)
+{
+ switch (qpad->fourcc) {
+ case FOURCC_apch:
+ case FOURCC_apcn:
+ case FOURCC_apcs:
+ case FOURCC_apco:
+ case FOURCC_ap4h:
+ case FOURCC_ap4x:
+ case FOURCC_c608:
+ case FOURCC_c708:
+ return gst_util_uint64_scale (qpad->sample_offset + 1,
+ qpad->expected_sample_duration_d * GST_SECOND,
+ qpad->expected_sample_duration_n);
+ case FOURCC_sowt:
+ case FOURCC_twos:{
+ guint64 block_idx;
+ guint64 next_sample_offset;
+
+ block_idx = prefill_get_block_index (qtmux, qpad);
+ next_sample_offset =
+ gst_util_uint64_scale (block_idx + 1,
+ qpad->expected_sample_duration_d *
+ atom_trak_get_timescale (qpad->trak),
+ qpad->expected_sample_duration_n);
+
+ return gst_util_uint64_scale (next_sample_offset, GST_SECOND,
+ atom_trak_get_timescale (qpad->trak));
+ }
+ default:
+ GST_ERROR_OBJECT (qtmux, "unsupported codec for pre-filling");
+ return -1;
+ }
+
+ return -1;
+}
+
+static GstBuffer *
+prefill_raw_audio_prepare_buf_func (GstQTPad * qtpad, GstBuffer * buf,
+ GstQTMux * qtmux)
+{
+ guint64 block_idx;
+ guint64 nsamples;
+ GstClockTime input_timestamp;
+ guint64 input_timestamp_distance;
+
+ if (buf)
+ gst_adapter_push (qtpad->raw_audio_adapter, buf);
+
+ block_idx = gst_util_uint64_scale_ceil (qtpad->raw_audio_adapter_offset,
+ qtpad->expected_sample_duration_n,
+ qtpad->expected_sample_duration_d *
+ atom_trak_get_timescale (qtpad->trak));
+ nsamples =
+ gst_util_uint64_scale (block_idx + 1,
+ qtpad->expected_sample_duration_d * atom_trak_get_timescale (qtpad->trak),
+ qtpad->expected_sample_duration_n) - qtpad->raw_audio_adapter_offset;
+
+ if ((!GST_COLLECT_PADS_STATE_IS_SET (&qtpad->collect,
+ GST_COLLECT_PADS_STATE_EOS)
+ && gst_adapter_available (qtpad->raw_audio_adapter) <
+ nsamples * qtpad->sample_size)
+ || gst_adapter_available (qtpad->raw_audio_adapter) == 0) {
+ return NULL;
+ }
+
+ input_timestamp =
+ gst_adapter_prev_pts (qtpad->raw_audio_adapter,
+ &input_timestamp_distance);
+ if (input_timestamp != GST_CLOCK_TIME_NONE)
+ input_timestamp +=
+ gst_util_uint64_scale (input_timestamp_distance, GST_SECOND,
+ qtpad->sample_size * atom_trak_get_timescale (qtpad->trak));
+
+ buf =
+ gst_adapter_take_buffer (qtpad->raw_audio_adapter,
+ !GST_COLLECT_PADS_STATE_IS_SET (&qtpad->collect,
+ GST_COLLECT_PADS_STATE_EOS) ? nsamples *
+ qtpad->sample_size : gst_adapter_available (qtpad->raw_audio_adapter));
+ GST_BUFFER_PTS (buf) = input_timestamp;
+ GST_BUFFER_DTS (buf) = GST_CLOCK_TIME_NONE;
+ GST_BUFFER_DURATION (buf) = GST_CLOCK_TIME_NONE;
+
+ qtpad->raw_audio_adapter_offset += nsamples;
+
+ /* Check if we have yet another block of raw audio in the adapter */
+ nsamples =
+ gst_util_uint64_scale (block_idx + 2,
+ qtpad->expected_sample_duration_d * atom_trak_get_timescale (qtpad->trak),
+ qtpad->expected_sample_duration_n) - qtpad->raw_audio_adapter_offset;
+ if (gst_adapter_available (qtpad->raw_audio_adapter) >=
+ nsamples * qtpad->sample_size) {
+ input_timestamp =
+ gst_adapter_prev_pts (qtpad->raw_audio_adapter,
+ &input_timestamp_distance);
+ if (input_timestamp != GST_CLOCK_TIME_NONE)
+ input_timestamp +=
+ gst_util_uint64_scale (input_timestamp_distance, GST_SECOND,
+ qtpad->sample_size * atom_trak_get_timescale (qtpad->trak));
+ qtpad->raw_audio_adapter_pts = input_timestamp;
+ } else {
+ qtpad->raw_audio_adapter_pts = GST_CLOCK_TIME_NONE;
+ }
+
+ return buf;
+}
+
+static void
+find_video_sample_duration (GstQTMux * qtmux, guint * dur_n, guint * dur_d)
+{
+ GSList *walk;
+
+ /* Find the (first) video track and assume that we have to output
+ * in that size */
+ for (walk = qtmux->collect->data; walk; walk = g_slist_next (walk)) {
+ GstCollectData *cdata = (GstCollectData *) walk->data;
+ GstQTPad *tmp_qpad = (GstQTPad *) cdata;
+
+ if (tmp_qpad->trak->is_video) {
+ *dur_n = tmp_qpad->expected_sample_duration_n;
+ *dur_d = tmp_qpad->expected_sample_duration_d;
+ break;
+ }
+ }
+
+ if (walk == NULL) {
+ GST_INFO_OBJECT (qtmux,
+ "Found no video framerate, using 40ms audio buffers");
+ *dur_n = 25;
+ *dur_d = 1;
+ }
+}
+
+/* Called when all pads are prerolled to adjust and */
+static gboolean
+prefill_update_sample_size (GstQTMux * qtmux, GstQTPad * qpad)
+{
+ switch (qpad->fourcc) {
+ case FOURCC_apch:
+ case FOURCC_apcn:
+ case FOURCC_apcs:
+ case FOURCC_apco:
+ case FOURCC_ap4h:
+ case FOURCC_ap4x:
+ {
+ guint sample_size = prefill_get_sample_size (qtmux, qpad);
+ atom_trak_set_constant_size_samples (qpad->trak, sample_size);
+ return TRUE;
+ }
+ case FOURCC_c608:
+ case FOURCC_c708:
+ {
+ guint sample_size = prefill_get_sample_size (qtmux, qpad);
+ /* We need a "valid" duration */
+ find_video_sample_duration (qtmux, &qpad->expected_sample_duration_n,
+ &qpad->expected_sample_duration_d);
+ atom_trak_set_constant_size_samples (qpad->trak, sample_size);
+ return TRUE;
+ }
+ case FOURCC_sowt:
+ case FOURCC_twos:{
+ find_video_sample_duration (qtmux, &qpad->expected_sample_duration_n,
+ &qpad->expected_sample_duration_d);
+ /* Set a prepare_buf_func that ensures this */
+ qpad->prepare_buf_func = prefill_raw_audio_prepare_buf_func;
+ qpad->raw_audio_adapter = gst_adapter_new ();
+ qpad->raw_audio_adapter_offset = 0;
+ qpad->raw_audio_adapter_pts = GST_CLOCK_TIME_NONE;
+
+ return TRUE;
+ }
+ default:
+ return TRUE;
+ }
+}
+
+/* Only called at startup when doing the "fake" iteration of all tracks in order
+ * to prefill the sample tables in the header. */
+static GstQTPad *
+find_best_pad_prefill_start (GstQTMux * qtmux)
+{
+ GSList *walk;
+ GstQTPad *best_pad = NULL;
+
+ /* If interleave limits have been specified and the current pad is within
+ * those interleave limits, pick that one, otherwise let's try to figure out
+ * the next best one. */
+ if (qtmux->current_pad &&
+ (qtmux->interleave_bytes != 0 || qtmux->interleave_time != 0) &&
+ (qtmux->interleave_bytes == 0
+ || qtmux->current_chunk_size <= qtmux->interleave_bytes)
+ && (qtmux->interleave_time == 0
+ || qtmux->current_chunk_duration <= qtmux->interleave_time)
+ && qtmux->mux_mode != GST_QT_MUX_MODE_FRAGMENTED
+ && qtmux->mux_mode != GST_QT_MUX_MODE_FRAGMENTED_STREAMABLE) {
+
+ if (qtmux->current_pad->total_duration < qtmux->reserved_max_duration) {
+ best_pad = qtmux->current_pad;
+ }
+ } else if (qtmux->collect->data->next) {
+ /* Attempt to try another pad if we have one. Otherwise use the only pad
+ * present */
+ best_pad = qtmux->current_pad = NULL;
+ }
+
+ /* The next best pad is the one which has the lowest timestamp and hasn't
+ * exceeded the reserved max duration */
+ if (!best_pad) {
+ GstClockTime best_time = GST_CLOCK_TIME_NONE;
+
+ for (walk = qtmux->collect->data; walk; walk = g_slist_next (walk)) {
+ GstCollectData *cdata = (GstCollectData *) walk->data;
+ GstQTPad *qtpad = (GstQTPad *) cdata;
+ GstClockTime timestamp;
+
+ if (qtpad->total_duration >= qtmux->reserved_max_duration)
+ continue;
+
+ timestamp = qtpad->total_duration;
+
+ if (best_pad == NULL ||
+ !GST_CLOCK_TIME_IS_VALID (best_time) || timestamp < best_time) {
+ best_pad = qtpad;
+ best_time = timestamp;
+ }
+ }
+ }
+
+ return best_pad;
+}
+
+/* Called when starting the file in prefill_mode to figure out all the entries
+ * of the header based on the input stream and reserved maximum duration.
+ *
+ * The _actual_ header (i.e. with the proper duration and trimmed sample tables)
+ * will be updated and written on EOS. */
+static gboolean
+gst_qt_mux_prefill_samples (GstQTMux * qtmux)
+{
+ GstQTPad *qpad;
+ GSList *walk;
+ GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
+
+ /* Update expected sample sizes/durations as needed, this is for raw
+ * audio where samples are actual audio samples. */
+ for (walk = qtmux->collect->data; walk; walk = g_slist_next (walk)) {
+ GstCollectData *cdata = (GstCollectData *) walk->data;
+ GstQTPad *qpad = (GstQTPad *) cdata;
+
+ if (!prefill_update_sample_size (qtmux, qpad))
+ return FALSE;
+ }
+
+ if (qtmux_klass->format == GST_QT_MUX_FORMAT_QT) {
+ /* For the first sample check/update timecode as needed. We do that before
+ * all actual samples as the code in gst_qt_mux_add_buffer() does it with
+ * initial buffer directly, not with last_buf */
+ for (walk = qtmux->collect->data; walk; walk = g_slist_next (walk)) {
+ GstCollectData *cdata = (GstCollectData *) walk->data;
+ GstQTPad *qpad = (GstQTPad *) cdata;
+ GstBuffer *buffer =
+ gst_collect_pads_peek (qtmux->collect, (GstCollectData *) qpad);
+ GstVideoTimeCodeMeta *tc_meta;
+
+ if (buffer && (tc_meta = gst_buffer_get_video_time_code_meta (buffer))
+ && qpad->trak->is_video) {
+ GstVideoTimeCode *tc = &tc_meta->tc;
+
+ qpad->tc_trak = atom_trak_new (qtmux->context);
+ atom_moov_add_trak (qtmux->moov, qpad->tc_trak);
+
+ qpad->trak->tref = atom_tref_new (FOURCC_tmcd);
+ atom_tref_add_entry (qpad->trak->tref, qpad->tc_trak->tkhd.track_ID);
+
+ atom_trak_set_timecode_type (qpad->tc_trak, qtmux->context,
+ qpad->trak->mdia.mdhd.time_info.timescale, tc);
+
+ atom_trak_add_samples (qpad->tc_trak, 1, 1, 4,
+ qtmux->mdat_size, FALSE, 0);
+
+ qpad->tc_pos = qtmux->mdat_size;
+ qpad->first_tc = gst_video_time_code_copy (tc);
+ qpad->first_pts = GST_BUFFER_PTS (buffer);
+
+ qtmux->current_chunk_offset = -1;
+ qtmux->current_chunk_size = 0;
+ qtmux->current_chunk_duration = 0;
+ qtmux->mdat_size += 4;
+ }
+ if (buffer)
+ gst_buffer_unref (buffer);
+ }
+ }
+
+ while ((qpad = find_best_pad_prefill_start (qtmux))) {
+ GstClockTime timestamp, next_timestamp, duration;
+ guint nsamples, sample_size;
+ guint64 chunk_offset;
+ gint64 scaled_duration;
+ gint64 pts_offset = 0;
+ gboolean sync = FALSE;
+ TrakBufferEntryInfo sample_entry;
+
+ sample_size = prefill_get_sample_size (qtmux, qpad);
+
+ if (sample_size == -1) {
+ return FALSE;
+ }
+
+ if (!qpad->samples)
+ qpad->samples = g_array_new (FALSE, FALSE, sizeof (TrakBufferEntryInfo));
+
+ timestamp = qpad->total_duration;
+ next_timestamp = prefill_get_next_timestamp (qtmux, qpad);
+ duration = next_timestamp - timestamp;
+
+ if (qpad->first_ts == GST_CLOCK_TIME_NONE)
+ qpad->first_ts = timestamp;
+ if (qpad->first_dts == GST_CLOCK_TIME_NONE)
+ qpad->first_dts = timestamp;
+
+ if (qtmux->current_pad != qpad || qtmux->current_chunk_offset == -1) {
+ qtmux->current_pad = qpad;
+ if (qtmux->current_chunk_offset == -1)
+ qtmux->current_chunk_offset = qtmux->mdat_size;
+ else
+ qtmux->current_chunk_offset += qtmux->current_chunk_size;
+ qtmux->current_chunk_size = 0;
+ qtmux->current_chunk_duration = 0;
+ }
+ if (qpad->sample_size)
+ nsamples = sample_size / qpad->sample_size;
+ else
+ nsamples = 1;
+ qpad->last_dts = timestamp;
+ scaled_duration = gst_util_uint64_scale_round (timestamp + duration,
+ atom_trak_get_timescale (qpad->trak),
+ GST_SECOND) - gst_util_uint64_scale_round (timestamp,
+ atom_trak_get_timescale (qpad->trak), GST_SECOND);
+
+ qtmux->current_chunk_size += sample_size;
+ qtmux->current_chunk_duration += duration;
+ qpad->total_bytes += sample_size;
+
+ chunk_offset = qtmux->current_chunk_offset;
+
+ /* I-frame only, no frame reordering */
+ sync = FALSE;
+ pts_offset = 0;
+
+ if (qtmux->current_chunk_duration > qtmux->longest_chunk
+ || !GST_CLOCK_TIME_IS_VALID (qtmux->longest_chunk)) {
+ qtmux->longest_chunk = qtmux->current_chunk_duration;
+ }
+
+ sample_entry.track_id = qpad->trak->tkhd.track_ID;
+ sample_entry.nsamples = nsamples;
+ sample_entry.delta = scaled_duration / nsamples;
+ sample_entry.size = sample_size / nsamples;
+ sample_entry.chunk_offset = chunk_offset;
+ sample_entry.pts_offset = pts_offset;
+ sample_entry.sync = sync;
+ sample_entry.do_pts = TRUE;
+ g_array_append_val (qpad->samples, sample_entry);
+ atom_trak_add_samples (qpad->trak, nsamples, scaled_duration / nsamples,
+ sample_size / nsamples, chunk_offset, sync, pts_offset);
+
+ qpad->total_duration = next_timestamp;
+ qtmux->mdat_size += sample_size;
+ qpad->sample_offset += nsamples;
+ }
+
+ return TRUE;
+}
+