| @@ -57,6 +57,8 @@ · 56 unmodified lines |
| 57 | #include "hevc.h" | 57 | #include "hevc.h" |
| 58 | #include "rtpenc.h" | 58 | #include "rtpenc.h" |
| 59 | #include "mov_chan.h" | 59 | #include "mov_chan.h" |
| | 60 | #include "movenc_ttml.h" |
| | 61 | #include "ttmlenc.h" |
| 60 | #include "vpcc.h" | 62 | #include "vpcc.h" |
| 61 | | 63 | |
| 62 | static const AVOption options[] = { | 64 | static const AVOption options[] = { |
| @@ -120,6 +122,7 @@ static const AVClass mov_isobmff_muxer_class = { · 57 unmodified lines |
| 120 | }; | 122 | }; |
| 121 | | 123 | |
| 122 | static int get_moov_size(AVFormatContext *s); | 124 | static int get_moov_size(AVFormatContext *s); |
| | 125 | static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt); |
| 123 | | 126 | |
| 124 | static int utf8len(const uint8_t *b) | 127 | static int utf8len(const uint8_t *b) |
| 125 | { | 128 | { |
| @@ -1788,7 +1791,29 @@ static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track) · 1662 unmodified lines |
| 1788 | | 1791 | |
| 1789 | if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE) | 1792 | if (track->par->codec_id == AV_CODEC_ID_DVD_SUBTITLE) |
| 1790 | mov_write_esds_tag(pb, track); | 1793 | mov_write_esds_tag(pb, track); |
| 1791 | else if (track->par->extradata_size) | 1794 | else if (track->par->codec_id == AV_CODEC_ID_TTML) { |
| | 1795 | switch (track->par->codec_tag) { |
| | 1796 | case MOV_ISMV_TTML_TAG: |
| | 1797 | // ISMV dfxp requires no extradata. |
| | 1798 | break; |
| | 1799 | case MOV_MP4_TTML_TAG: |
| | 1800 | // As specified in 14496-30, XMLSubtitleSampleEntry |
| | 1801 | // Namespace |
| | 1802 | avio_put_str(pb, "http://www.w3.org/ns/ttml"); |
| | 1803 | // Empty schema_location |
| | 1804 | avio_w8(pb, 0); |
| | 1805 | // Empty auxiliary_mime_types |
| | 1806 | avio_w8(pb, 0); |
| | 1807 | break; |
| | 1808 | default: |
| | 1809 | av_log(NULL, AV_LOG_ERROR, |
| | 1810 | "Unknown codec tag '%s' utilized for TTML stream with " |
| | 1811 | "index %d (track id %d)!\n", |
| | 1812 | av_fourcc2str(track->par->codec_tag), track->st->index, |
| | 1813 | track->track_id); |
| | 1814 | return AVERROR(EINVAL); |
| | 1815 | } |
| | 1816 | } else if (track->par->extradata_size) |
| 1792 | avio_write(pb, track->par->extradata, track->par->extradata_size); | 1817 | avio_write(pb, track->par->extradata, track->par->extradata_size); |
| 1793 | | 1818 | |
| 1794 | if (track->mode == MODE_MP4 && | 1819 | if (track->mode == MODE_MP4 && |
| @@ -2662,6 +2687,14 @@ static int mov_write_nmhd_tag(AVIOContext *pb) · 867 unmodified lines |
| 2662 | return 12; | 2687 | return 12; |
| 2663 | } | 2688 | } |
| 2664 | | 2689 | |
| | 2690 | static int mov_write_sthd_tag(AVIOContext *pb) |
| | 2691 | { |
| | 2692 | avio_wb32(pb, 12); |
| | 2693 | ffio_wfourcc(pb, "sthd"); |
| | 2694 | avio_wb32(pb, 0); |
| | 2695 | return 12; |
| | 2696 | } |
| | 2697 | |
| 2665 | static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track) | 2698 | static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track) |
| 2666 | { | 2699 | { |
| 2667 | int64_t pos = avio_tell(pb); | 2700 | int64_t pos = avio_tell(pb); |
| @@ -2788,6 +2821,8 @@ static int mov_write_hdlr_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra · 120 unmodified lines |
| 2788 | hdlr_type = "sbtl"; | 2821 | hdlr_type = "sbtl"; |
| 2789 | } else if (track->tag == MKTAG('m','p','4','s')) { | 2822 | } else if (track->tag == MKTAG('m','p','4','s')) { |
| 2790 | hdlr_type = "subp"; | 2823 | hdlr_type = "subp"; |
| | 2824 | } else if (track->tag == MOV_MP4_TTML_TAG) { |
| | 2825 | hdlr_type = "subt"; |
| 2791 | } else { | 2826 | } else { |
| 2792 | hdlr_type = "text"; | 2827 | hdlr_type = "text"; |
| 2793 | } | 2828 | } |
| @@ -2866,6 +2901,8 @@ static int mov_write_minf_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContext · 72 unmodified lines |
| 2866 | else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) { | 2901 | else if (track->par->codec_type == AVMEDIA_TYPE_SUBTITLE) { |
| 2867 | if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) { | 2902 | if (track->tag == MKTAG('t','e','x','t') || is_clcp_track(track)) { |
| 2868 | mov_write_gmhd_tag(pb, track); | 2903 | mov_write_gmhd_tag(pb, track); |
| | 2904 | } else if (track->tag == MOV_MP4_TTML_TAG) { |
| | 2905 | mov_write_sthd_tag(pb); |
| 2869 | } else { | 2906 | } else { |
| 2870 | mov_write_nmhd_tag(pb); | 2907 | mov_write_nmhd_tag(pb); |
| 2871 | } | 2908 | } |
| @@ -5251,6 +5288,68 @@ static int mov_flush_fragment_interleaving(AVFormatContext *s, MOVTrack *track) · 2379 unmodified lines |
| 5251 | return 0; | 5288 | return 0; |
| 5252 | } | 5289 | } |
| 5253 | | 5290 | |
| | 5291 | static int mov_write_squashed_packet(AVFormatContext *s, MOVTrack *track) |
| | 5292 | { |
| | 5293 | MOVMuxContext *mov = s->priv_data; |
| | 5294 | AVPacket *squashed_packet = mov->pkt; |
| | 5295 | int ret = AVERROR_BUG; |
| | 5296 | |
| | 5297 | switch (track->st->codecpar->codec_id) { |
| | 5298 | case AV_CODEC_ID_TTML: { |
| | 5299 | int had_packets = !!track->squashed_packet_queue; |
| | 5300 | |
| | 5301 | if ((ret = ff_mov_generate_squashed_ttml_packet(s, track, squashed_packet)) < 0) { |
| | 5302 | goto finish_squash; |
| | 5303 | } |
| | 5304 | |
| | 5305 | // We have generated a padding packet (no actual input packets in |
| | 5306 | // queue) and its duration is zero. Skipping writing it. |
| | 5307 | if (!had_packets && squashed_packet->duration == 0) { |
| | 5308 | goto finish_squash; |
| | 5309 | } |
| | 5310 | |
| | 5311 | track->end_reliable = 1; |
| | 5312 | break; |
| | 5313 | } |
| | 5314 | default: |
| | 5315 | ret = AVERROR(EINVAL); |
| | 5316 | goto finish_squash; |
| | 5317 | } |
| | 5318 | |
| | 5319 | squashed_packet->stream_index = track->st->index; |
| | 5320 | |
| | 5321 | ret = mov_write_single_packet(s, squashed_packet); |
| | 5322 | |
| | 5323 | finish_squash: |
| | 5324 | av_packet_unref(squashed_packet); |
| | 5325 | |
| | 5326 | return ret; |
| | 5327 | } |
| | 5328 | |
| | 5329 | static int mov_write_squashed_packets(AVFormatContext *s) |
| | 5330 | { |
| | 5331 | MOVMuxContext *mov = s->priv_data; |
| | 5332 | |
| | 5333 | for (int i = 0; i < s->nb_streams; i++) { |
| | 5334 | MOVTrack *track = &mov->tracks[i]; |
| | 5335 | int ret = AVERROR_BUG; |
| | 5336 | |
| | 5337 | if (track->squash_fragment_samples_to_one && !track->entry) { |
| | 5338 | if ((ret = mov_write_squashed_packet(s, track)) < 0) { |
| | 5339 | av_log(s, AV_LOG_ERROR, |
| | 5340 | "Failed to write squashed packet for %s stream with " |
| | 5341 | "index %d and track id %d. Error: %s\n", |
| | 5342 | avcodec_get_name(track->st->codecpar->codec_id), |
| | 5343 | track->st->index, track->track_id, |
| | 5344 | av_err2str(ret)); |
| | 5345 | return ret; |
| | 5346 | } |
| | 5347 | } |
| | 5348 | } |
| | 5349 | |
| | 5350 | return 0; |
| | 5351 | } |
| | 5352 | |
| 5254 | static int mov_flush_fragment(AVFormatContext *s, int force) | 5353 | static int mov_flush_fragment(AVFormatContext *s, int force) |
| 5255 | { | 5354 | { |
| 5256 | MOVMuxContext *mov = s->priv_data; | 5355 | MOVMuxContext *mov = s->priv_data; |
| @@ -5262,6 +5361,11 @@ static int mov_flush_fragment(AVFormatContext *s, int force) · 5 unmodified lines |
| 5262 | if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) | 5361 | if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) |
| 5263 | return 0; | 5362 | return 0; |
| 5264 | | 5363 | |
| | 5364 | // Check if we have any tracks that require squashing. |
| | 5365 | // In that case, we'll have to write the packet here. |
| | 5366 | if ((ret = mov_write_squashed_packets(s)) < 0) |
| | 5367 | return ret; |
| | 5368 | |
| 5265 | // Try to fill in the duration of the last packet in each stream | 5369 | // Try to fill in the duration of the last packet in each stream |
| 5266 | // from queued packets in the interleave queues. If the flushing | 5370 | // from queued packets in the interleave queues. If the flushing |
| 5267 | // of fragments was triggered automatically by an AVPacket, we | 5371 | // of fragments was triggered automatically by an AVPacket, we |
| @@ -5727,7 +5831,8 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt) · 459 unmodified lines |
| 5727 | trk->cluster[trk->entry].entries = samples_in_chunk; | 5831 | trk->cluster[trk->entry].entries = samples_in_chunk; |
| 5728 | trk->cluster[trk->entry].dts = pkt->dts; | 5832 | trk->cluster[trk->entry].dts = pkt->dts; |
| 5729 | trk->cluster[trk->entry].pts = pkt->pts; | 5833 | trk->cluster[trk->entry].pts = pkt->pts; |
| 5730 | if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) { | 5834 | if (!trk->squash_fragment_samples_to_one && |
| | 5835 | !trk->entry && trk->start_dts != AV_NOPTS_VALUE) { |
| 5731 | if (!trk->frag_discont) { | 5836 | if (!trk->frag_discont) { |
| 5732 | /* First packet of a new fragment. We already wrote the duration | 5837 | /* First packet of a new fragment. We already wrote the duration |
| 5733 | * of the last packet of the previous fragment based on track_duration, | 5838 | * of the last packet of the previous fragment based on track_duration, |
| @@ -6022,6 +6127,33 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt) · 288 unmodified lines |
| 6022 | } | 6127 | } |
| 6023 | } | 6128 | } |
| 6024 | | 6129 | |
| | 6130 | if (trk->squash_fragment_samples_to_one) { |
| | 6131 | /* |
| | 6132 | * If the track has to have its samples squashed into one sample, |
| | 6133 | * we just take it into the track's queue. |
| | 6134 | * This will then be utilized as the samples get written in either |
| | 6135 | * mov_flush_fragment or when the mux is finalized in |
| | 6136 | * mov_write_trailer. |
| | 6137 | */ |
| | 6138 | int ret = AVERROR_BUG; |
| | 6139 | |
| | 6140 | if (pkt->pts == AV_NOPTS_VALUE) { |
| | 6141 | av_log(s, AV_LOG_ERROR, |
| | 6142 | "Packets without a valid presentation timestamp are " |
| | 6143 | "not supported with packet squashing!\n"); |
| | 6144 | return AVERROR(EINVAL); |
| | 6145 | } |
| | 6146 | |
| | 6147 | if ((ret = avpriv_packet_list_put(&trk->squashed_packet_queue, |
| | 6148 | &trk->squashed_packet_queue_end, |
| | 6149 | pkt, av_packet_ref, 0)) < 0) { |
| | 6150 | return ret; |
| | 6151 | } |
| | 6152 | |
| | 6153 | return 0; |
| | 6154 | } |
| | 6155 | |
| | 6156 | |
| 6025 | if (trk->mode == MODE_MOV && trk->par->codec_type == AVMEDIA_TYPE_VIDEO) { | 6157 | if (trk->mode == MODE_MOV && trk->par->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 6026 | AVPacket *opkt = pkt; | 6158 | AVPacket *opkt = pkt; |
| 6027 | int reshuffle_ret, ret; | 6159 | int reshuffle_ret, ret; |
| @@ -6300,6 +6432,11 @@ static void mov_free(AVFormatContext *s) · 272 unmodified lines |
| 6300 | | 6432 | |
| 6301 | ff_mov_cenc_free(&mov->tracks[i].cenc); | 6433 | ff_mov_cenc_free(&mov->tracks[i].cenc); |
| 6302 | ffio_free_dyn_buf(&mov->tracks[i].mdat_buf); | 6434 | ffio_free_dyn_buf(&mov->tracks[i].mdat_buf); |
| | 6435 | |
| | 6436 | if (mov->tracks[i].squashed_packet_queue) { |
| | 6437 | avpriv_packet_list_free(&(mov->tracks[i].squashed_packet_queue), |
| | 6438 | &(mov->tracks[i].squashed_packet_queue_end)); |
| | 6439 | } |
| 6303 | } | 6440 | } |
| 6304 | | 6441 | |
| 6305 | av_freep(&mov->tracks); | 6442 | av_freep(&mov->tracks); |
| @@ -6690,6 +6827,36 @@ static int mov_init(AVFormatContext *s) · 384 unmodified lines |
| 6690 | } | 6827 | } |
| 6691 | } else if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) { | 6828 | } else if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) { |
| 6692 | track->timescale = st->time_base.den; | 6829 | track->timescale = st->time_base.den; |
| | 6830 | |
| | 6831 | if (track->par->codec_id == AV_CODEC_ID_TTML) { |
| | 6832 | /* 14496-30 requires us to use a single sample per fragment |
| | 6833 | for TTML, for which we define a per-track flag. |
| | 6834 | |
| | 6835 | We set the flag in case we are receiving TTML paragraphs |
| | 6836 | from the input, in other words in case we are not doing |
| | 6837 | stream copy. */ |
| | 6838 | track->squash_fragment_samples_to_one = |
| | 6839 | ff_is_ttml_stream_paragraph_based(track->par); |
| | 6840 | |
| | 6841 | if (mov->flags & FF_MOV_FLAG_FRAGMENT && |
| | 6842 | track->squash_fragment_samples_to_one) { |
| | 6843 | av_log(s, AV_LOG_ERROR, |
| | 6844 | "Fragmentation is not currently supported for " |
| | 6845 | "TTML in MP4/ISMV (track synchronization between " |
| | 6846 | "subtitles and other media is not yet implemented)!\n"); |
| | 6847 | return AVERROR_PATCHWELCOME; |
| | 6848 | } |
| | 6849 | |
| | 6850 | if (track->mode != MODE_ISM && |
| | 6851 | track->par->codec_tag == MOV_ISMV_TTML_TAG && |
| | 6852 | s->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) { |
| | 6853 | av_log(s, AV_LOG_ERROR, |
| | 6854 | "ISMV style TTML support with the 'dfxp' tag in " |
| | 6855 | "non-ISMV formats is not officially supported. Add " |
| | 6856 | "'-strict unofficial' if you want to use it.\n"); |
| | 6857 | return AVERROR_EXPERIMENTAL; |
| | 6858 | } |
| | 6859 | } |
| 6693 | } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) { | 6860 | } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) { |
| 6694 | track->timescale = st->time_base.den; | 6861 | track->timescale = st->time_base.den; |
| 6695 | } else { | 6862 | } else { |
| @@ -7036,6 +7203,11 @@ static int mov_write_trailer(AVFormatContext *s) · 340 unmodified lines |
| 7036 | } | 7203 | } |
| 7037 | } | 7204 | } |
| 7038 | | 7205 | |
| | 7206 | // Check if we have any tracks that require squashing. |
| | 7207 | // In that case, we'll have to write the packet here. |
| | 7208 | if ((res = mov_write_squashed_packets(s)) < 0) |
| | 7209 | return res; |
| | 7210 | |
| 7039 | // If there were no chapters when the header was written, but there | 7211 | // If there were no chapters when the header was written, but there |
| 7040 | // are chapters now, write them in the trailer. This only works | 7212 | // are chapters now, write them in the trailer. This only works |
| 7041 | // when we are not doing fragments. | 7213 | // when we are not doing fragments. |
| @@ -7180,6 +7352,8 @@ static const AVCodecTag codec_mp4_tags[] = { · 138 unmodified lines |
| 7180 | { AV_CODEC_ID_MOV_TEXT, MKTAG('t', 'x', '3', 'g') }, | 7352 | { AV_CODEC_ID_MOV_TEXT, MKTAG('t', 'x', '3', 'g') }, |
| 7181 | { AV_CODEC_ID_BIN_DATA, MKTAG('g', 'p', 'm', 'd') }, | 7353 | { AV_CODEC_ID_BIN_DATA, MKTAG('g', 'p', 'm', 'd') }, |
| 7182 | { AV_CODEC_ID_MPEGH_3D_AUDIO, MKTAG('m', 'h', 'm', '1') }, | 7354 | { AV_CODEC_ID_MPEGH_3D_AUDIO, MKTAG('m', 'h', 'm', '1') }, |
| | 7355 | { AV_CODEC_ID_TTML, MOV_MP4_TTML_TAG }, |
| | 7356 | { AV_CODEC_ID_TTML, MOV_ISMV_TTML_TAG }, |
| 7183 | { AV_CODEC_ID_NONE, 0 }, | 7357 | { AV_CODEC_ID_NONE, 0 }, |
| 7184 | }; | 7358 | }; |
| 7185 | #if CONFIG_MP4_MUXER || CONFIG_PSP_MUXER | 7359 | #if CONFIG_MP4_MUXER || CONFIG_PSP_MUXER |
| @@ -7188,6 +7362,7 @@ static const AVCodecTag *const mp4_codec_tags_list[] = { codec_mp4_tags, NULL }; · 2 unmodified lines |
| 7188 | | 7362 | |
| 7189 | static const AVCodecTag codec_ism_tags[] = { | 7363 | static const AVCodecTag codec_ism_tags[] = { |
| 7190 | { AV_CODEC_ID_WMAPRO , MKTAG('w', 'm', 'a', ' ') }, | 7364 | { AV_CODEC_ID_WMAPRO , MKTAG('w', 'm', 'a', ' ') }, |
| | 7365 | { AV_CODEC_ID_TTML , MOV_ISMV_TTML_TAG }, |
| 7191 | { AV_CODEC_ID_NONE , 0 }, | 7366 | { AV_CODEC_ID_NONE , 0 }, |
| 7192 | }; | 7367 | }; |
| 7193 | | 7368 | |