WIP: av1_in_ts_v2 #21307
No reviewers
Labels
No labels
avcodec
avdevice
avfilter
avformat
avutil
backport/release/4.3
backport/release/4.4
backport/release/5.0
backport/release/5.1
backport/release/6.0
backport/release/6.1
backport/release/7.0
backport/release/7.1
backport/release/8.0
backport/release/8.1
backport/release/9.0
backport/release/9.1
CLI
doc
Forgejo Fairy
swresample
swscale
vulkan
API
API major
bug
enhancement
fix/bug
fix/regression
GSoC
GSoC-Qualification
help wanted
important
LGTM
meta
needs backport
needs docs
needs fate test
needs info
needs sample
needs testing
new
question
regression
repro
flaky
repro
no
repro
no(env)
repro
yes
resolution
duplicate
resolution
external
resolution
fixed
resolution
invalid
resolution
wontfix
security
small
No milestone
No assignees
8 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
FFmpeg/FFmpeg!21307
Loading…
Reference in a new issue
No description provided.
Delete branch "mypopydev/FFmpeg:av1_in_ts_v2"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This patch series V2 adds support for carrying AV1 video streams in MPEG-2 Transport Stream
containers, following the AOM "Carriage of AV1 in MPEG-2 TS" specification.
Key Features
where each OBU is prefixed with a 0x000001 start code. Includes Registration Descriptor ('AV01')
and AV1 Video Descriptor (0x80) in PMT.
Registration Descriptor. Outputs AV1 data in start code format.
format for compatibility with other containers and decoders. Automatically inserted when
remuxing to MP4, MKV, FLV, IVF, and raw OBU files.
@ -146,6 +146,7 @@#define STREAM_TYPE_VIDEO_HEVC 0x24#define STREAM_TYPE_VIDEO_JPEGXS 0x32#define STREAM_TYPE_VIDEO_VVC 0x33#define STREAM_TYPE_VIDEO_AV1 0x06 /* Per AOM AV1-MPEG2-TS draft spec */Do not add this, simply use PRIVATE_DATA.
@ -376,1 +459,4 @@break;case AV_CODEC_ID_AV1:stream_type = STREAM_TYPE_PRIVATE_DATA;break;There is already a branch below which returns STREAM_TYPE_PRIVATE_DATA.
make sense, will chaneg, thx
fixed, thx
@ -807,6 +893,9 @@ static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)put_registration_descriptor(&q, MKTAG('V', 'C', '-', '1'));} else if (stream_type == STREAM_TYPE_VIDEO_HEVC && s->strict_std_compliance <= FF_COMPLIANCE_NORMAL) {put_registration_descriptor(&q, MKTAG('H', 'E', 'V', 'C'));} else if (codec_id == AV_CODEC_ID_AV1 && s->strict_std_compliance <= FF_COMPLIANCE_NORMAL) {I don't think the complicance check is needed here, it was added for HEVC because the HEVC registration descriptor is not standard, but improves compatiblity.
Ok
changed as review
The specs is still a draft. How close is to become final?
3f86ff1701486e1b8355Docs lgtm
@ -95,0 +111,4 @@@itemize@itemConvert an AV1 stream from MPEG-TS to MP4:"Convert an AV1 stream from MPEG-TS to MP4" --> "Remux an AV1 stream from MPEG-TS into MP4"
Ok, I don't have any particular preference, as a non-native English speaker.
486e1b8355ffbededa06@cus wrote in #21307 (comment):
Actually, I don't know for sure, but we've already started using the spec. Additionally, we need an implementation to cross-validate with the spec.
@ -354,0 +427,4 @@(seq.chroma_sample_position & 0x03);/* Byte 3: hdr_wcg_idc (2) | reserved_zeros (1) | initial_presentation_delay_present (1) |* reserved_zeros (4) *//* We don't use initial_presentation_delay, so set to 0 */Surely you should use it from the bitstream if it's there?
I will add handling for the initial_presentation_delay field.
@ -1792,0 +1911,4 @@** Note: Per AOM AV1-MPEG2-TS spec section 3.6.2.1, emulation prevention bytes* should be handled, but for now we rely on the obu_size field for boundary* detection which makes emulation prevention optional in practice.That's not how emulation prevention works or the purpose of emulation prevention. It is not optional.
Now support the EPB (Emulation Prevention Byte), thx
In libavcodec you can use ff_startcode_find_candidate_c() and avpriv_find_start_code(), and in libavformat you can use the latter. No need to write yet another implementation for it.
ff_startcode_find_candidate_c() even has target specific optimized versions if you use function pointers for them, like how h264dsp.c and vc1dsp.c do.
Also, you can't use any ff_* symbol from libavcodec in libavformat, as they will not be available in shared builds.
@ -893,6 +894,7 @@ static const StreamType DESC_types[] = {{ AC3_DESCRIPTOR, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },{ ENHANCED_AC3_DESCRIPTOR, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },{ DTS_DESCRIPTOR, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },{ AV1_VIDEO_DESCRIPTOR, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_AV1 },Considering that AV1_VIDEO_DESCRIPTOR is in the User-Defined area, this should not be needed, and it should not be added here, otherwise other (future) codecs using the same 0x80 descriptor ID can be misdetected as AV1. The registration descriptor (its presence is mandatory in the stream) should be enough to detect the AV1 codec.
Yes, will remove thhis part, thx
@jamrial wrote in #21307 (comment):
Thanks for the review.
avpriv_find_start_code() is used in libavcodec/av1_parse.c for searching start codes when parsing OBUs.
No cross-library ff_* calls: libavformat/av1.c has independent OBU parsing implementation. All ff_av1_* functions are defined locally within each library, ensuring shared builds work correctly.
ffbededa063c18a3d38b3c18a3d38b60c0cf971860c0cf97181de25056081de2505608d74c13a088@ -53,2 +57,4 @@};/*** Convert start code format to Section 5 format for parsing.No, an AVCodecParser must not remove or replace data, and much less allocate buffers like you're doing here. It's only supposed to assemble data into complete packets if required/requested, and parsing them for stream parameters.
The start codes and emulation prevention 3bytes must be removed by a bsf, like the one you wrote, or a splitter like in av1_parse.c, one of which should be used by decoders and muxer.
See my comment in av1_parse.c for how to handle CBS support, so you don't need to touch this parser.
fixed
@ -172,0 +204,4 @@* @return 0 on success, negative error code on failure*/int ff_av1_packet_split_startcode(AV1Packet *pkt, const uint8_t *buf,int length, void *logctx);See https://code.ffmpeg.org/jamrial/FFmpeg/commits/branch/mpegts for an alternative (and more complete, as it removes the emulation prevention bytes) implementation of this.
I did not fully test the emulation prevention byte removal, fwiw.
I reused the patch from this branch, thx
@ -1006,0 +1033,4 @@@end exampleThe bitstream filter is automatically applied when needed.This is way too verbose to be in the MPEGTS demuxer user documentation. Some of this docs is for programmers working on mpegts demuxer code, some of it is already explained in the bitstream filter documentation. So I'd rather keep the mpegts demuxer docs unmodified and delete this whole chunk.
It will remain as it was before; I have no particular preference regarding this.
d74c13a088907f00c596907f00c5963bae92406bYou're still not adding emulation prevention bytes in the mpegts muxer. You're also not removing them from the packets after they are exported by the mpegts demuxer.
See the branch i linked in a previous comment for the latter, which is a cleaner and complete implementation of the splitter in av1_parse.c. av1_tstosection5 bsf can then use said updated splitter.
@jamrial wrote in #21307 (comment):
The next commit will be added, but first I need a nap :)
3bae92406b4fc83b36bc@jamrial wrote in #21307 (comment):
Now support the EPB (Emulation Prevention Byte) as you review and patch:
Muxing (libavformat/av1.c): av1_epb_count() calculates required EPBs for buffer allocation, then av1_write_with_epb() writes data with EPB insertion. Called by ff_av1_section5_to_startcode().
Demuxing (libavcodec/av1_parse.c): ff_av1_extract_obu() removes EPB during OBU extraction. When 00 00 03 is detected, the 03 byte is skipped. Result: escaped_data/escaped_size contains clean data (EPB removed), raw_data/raw_size contains original data.
thx
4fc83b36bc118d052138118d05213890a444e6ef@ -377,3 +479,1 @@int start_pos, type, temporal_id, spatial_id;int len = parse_obu_header(buf, size, &obu_size, &start_pos,&type, &temporal_id, &spatial_id);int len = obu_parse_header(buf, size, &obu_type, &obu_size, &header_len);Why are you reimplementing parse_obu_header() locally but with a different name? It's an inline function that can be used anywhere. Let's no duplicate stuff that can be trivially reused.
I mistakenly thought this was a cross-libs call.
@ -751,6 +751,7 @@ SHLIBOBJS-$(CONFIG_IMAGE_JPEGXL_PIPE_DEMUXER) += jpegxl_parse.oSHLIBOBJS-$(CONFIG_JNI) += ffjni.oSHLIBOBJS-$(CONFIG_JPEGXL_ANIM_DEMUXER) += jpegxl_parse.oSHLIBOBJS-$(CONFIG_MATROSKA_DEMUXER) += mpeg4audio_sample_rates.oSHLIBOBJS-$(CONFIG_MOV_MUXER) += cbs_av1.o startcode.o av1_parse.oAdding cbs_av1.o here is not required. cbs_av1.c in lavf includes cbs_av1.c from lavc.
If you want CBS writing support in lavf, remove the CBS_WRITE define from libavformat/cbs.h
Looking a bit more close, you don't need CBS writing support, so just add av1_parse.o to SHLIBOBJS-$(CONFIG_MOV_MUXER)
cbs_av1.o can be removed, but need to keep startcode.o (for ff_startcode_find_candidate_c) av1_parse.o
@ -1,3 +1,3 @@194cd796f3d28a890b516b2507f57180 *tests/data/lavf-fate/lavf.av1.mp455960 tests/data/lavf-fate/lavf.av1.mp4d6afef355cf1bcf1a71b9cfb98c32fba *tests/data/lavf-fate/lavf.av1.mp455976 tests/data/lavf-fate/lavf.av1.mp4This shouldn't happen. This test has nothing to do with mpeg-ts encapsulation, so it shouldn't insert av1_tstosection5 or be affected at all.
Yes,this change is due to the current parser's support for SAR updates, as noted in the review below
Yes, noticed after making this comment. The updated references should be in the same commit that introduced the relevant change.
I've messed up the submit granularity. Will fix this issue later.
@ -87,0 +104,4 @@static inline int av1_is_startcode_format(const uint8_t *buf, int size){return (size >= 3 && AV_RB24(buf) == 0x000001) ||(size >= 4 && AV_RB32(buf) == 0x00000001);I thought start code for av1 in mpegts was only 0x000001, and never 0x00000001.
From a specification perspective, yes. It's just that codecs like AVC and HEVC have included the same code verification logic as a feature for compatibility.
AVC, HEVC and VVC Annex B define start code 0x00000001 for parameter set NALUs, and 0x000001 for every other NALU type, so it's needed for them.
For AV1 in MPEG-TS, only 0x000001 is defined. Notice how it even mentions that 0x000000 shall not happen, so a four byte start code is invalid.
fixed, now implemented stricter checks in the code, as you suggested.
@ -789,0 +757,4 @@// For extradata, try removing trailing padding bytessize_t trimmed_size = size;while (trimmed_size > 0 && data[trimmed_size - 1] == 0x00) {trimmed_size--;Can you explain this? In what scenario extradata would have trailing zeroes and why would it make ff_av1_packet_split() fail?
fixed
@ -733,2 +729,2 @@goto fail;}// If version is not 1, this might be raw OBU data that happens to// have the high bit set. Try parsing it as raw OBU data instead.In what scenario the first byte in extradata could have the very first bit set with config_record_version != 1 that we could want to handle?
That bit being set is strictly for AV1CodecConfigurationRecord, so it should not be attempted to be parsed as anything else.
Will fix, thx
@ -136,1 +136,4 @@ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;/* Extract SAR from render_size if different from frame_size */if (av1->render_width && av1->render_height &&No need for this check. None of these will be zero. Also, don't use
av1->render_*, useframe->render_*_minus_1 + 1. The values inCodedBitstreamAV1Contextbelong to the last parsed frame in a TU, which may not be the shown one.This commit is the one changing the output of
fate-lavf-fate-av1.mp4andfate-lavf-fate-av1.mkv, so they should be updated here (And also, ensure the new output is correct).Will remove the check and use frame->render_*_minus_1 + 1, thx
@ -2328,0 +2440,4 @@* Most AV1 encoders (libaom, SVT-AV1) produce single-frame TUs, so the* current implementation works without av1_frame_split. For multi-frame TUs* (e.g., SVC content), users should manually apply av1_frame_split with* appropriate timestamp handling.This is not ok. First of all, all encoders produce files with more than one frame per TU depending on encoding parameters. What's more, the sample you used for the test, av1-1-b8-05-mv.ivf, does!
Muxing valid files can't depend on users knowing when to insert a bitstream filter and handle timestamps. Either we do it in a compliant way, or we don't do it at all.
So my suggestion is, if you can't implement https://aomediacodec.github.io/av1-mpeg2-ts/#assignment-of-dts-and-pts, then we should skip adding muxing altogether, and only introduce demuxing (Which should always use av1_frame_merge). FFmpeg must not be the source of an incomplete implementation and invalid files. The end result will be readers being written to handle libavformat created files rather than spec compliant files.
I haven't found a straightforward way to handle DTS and PTS in the MPEG-TS muxer. As a result, this commit doesn't support AV1 in the MPEG-TS muxer yet. I have validated other features using an experimental version of AV1 in the MPEG-TS muxer.
This crashes:
Bisect points to:
commit c1aba801a18b5006b750c045341ca74c686c260c
Author: Jun Zhao [email protected]
Date: Thu Jan 1 16:18:59 2026 +0800
@michaelni wrote in #21307 (comment):
A Fate test or some sample?
Could you kindly send me the reproduction command and the
corresponding test stream privately? @Michael Niedermayer
@mypopydev wrote in #21307 (comment):
sample sent privately to james and jun
the command line is boring its just
valgrind ./ffmpeg_g -i sample
@michaelni wrote in #21307 (comment):
fwiw, the failure happens in c1aba801a1 but not in my original commit
jamrial/FFmpeg@d235a48dc0that @mypopydev modified for this PR.@michaelni wrote in #21307 (comment):
thx,I will investigate the cause of this issue.
e3b2feff13028dfedf7a@jamrial wrote in #21307 (comment):
fixed, and fix a new crash by 03b93d6d2e
028dfedf7ae0b71d6f82e0b71d6f82ecaa9bad6a@jamrial wrote in #21307 (comment):
Any other comments?
ecaa9bad6afa447a5267@mypopydev wrote in #21307 (comment):
Ping :)
@ -1006,0 +1010,4 @@When remuxing to other containers (MP4, MKV, FLV, IVF, etc.), the@code{av1_tstosection5} bitstream filter is automatically inserted.IMHO this is still unneeded. Even mentioning AV1 support is questionable, we don't mention other codecs we support in mpegts, yet we support plenty...
I can simplify this part. thx
@cus wrote in #21307 (comment):
Done
Any other comments?thx @jamrial @cus
fa447a5267c1d816b01b@mypopydev Could you please test whether your codes work for av1_d3d12va encoder?
The sample command line:
ffmpeg -hwaccel d3d12va -hwaccel_output_format d3d12 -i input_h264.mp4 -c:v av1_d3d12va -rc_mode VBR -b:v 3M -g 120 -bf 0 -y av1_output.ts
Thx.
I found the av1 stream encoded by av1_d3d12va and av1_vaapi can't use mkv or mp4 container, there were errors, while there was s no such kind of issue for libaom-av1 encoder and .
I checked the syntax, I found they are different.
For av1_d3d12va and av1_vaapi, the structure is:
OBU_FRAME_HEADER
OBU_TILE_GROUP
OBU_TD
OBU_FRAME_HEADER
OBU_TILE_GROUP
OBU_TD
OBU_FRAME_HEADER
OBU_TILE_GROUP
OBU_TD
...
For libaom-av1, the structure is:
OBU_FRAME
OBU_FRAME
OBU_FRAME
...
I think the current mkv and mp4 container don't support the structure of av1_d3d12va and av1_vaapi although this structure is valid.
Because you are supporting mpeg2 ts container right now, maybe this information will be useful to you.
@younengxiao wrote in #21307 (comment):
Both those coded OBU orders are valid.
It should, i don't see why it would not, as long as it includes a sequence header in some form.
This PR does not include muxing in TS. Supporting that is not trivial.
It seems like this PR wants to add a second way for how AV1 is put in AVPackets, namely with the start code format, similarly to how we support ISOBMFF and annex B framing for H2645. But why should we add this complexity everywhere instead of only exporting the data in low-overhead format from every demuxer (even if this entails two unnecessary repacketization steps for certain remuxing scenarios)?
@mkver wrote in #21307 (comment):
Unless i understood it wrong, being in mpegts means packets may not be complete and require assembling. I'm not 100% sure if there's anything in the mpeg-ts spec that states packets should be complete and readers should read them fully, but at least for h26x this is not the case. And the fact this av1 in ts spec came up with the start code framing hints that it'd be the same here.
If the above is true, then we can't remove the start code framing within the demuxer, and instead require an AVParser to assemble full AUs, which will retain the start code and emulation prevention bytes. I already wrote one, but since there are no samples and seemingly nobody can say if this draft spec is actually used in real world scenarios, i didn't bother to post it.
@jamrial wrote in #21307 (comment):
"I'm not 100% sure if there's anything in the mpeg-ts spec that states packets should be complete and readers should read them fully, ": No, there is no this requirement in MPEG-2 TS (13818-1) spec.
There are two layer of packets in MPEG-2 TS: TS packet (usually fix size of 188 byte) and PES packet. One PES packet usually crosses multiple TS packets. One video frame can either in one PES packet, or cross more than 1 PES packet. One PES packet can carry one video frame or multiple video frames.
av1_in_ts_v2to WIP: av1_in_ts_v2View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.