From: Mark Thompson <sw@jkqxz.net> To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH v3 3/7] lavf: APV demuxer Date: Wed, 23 Apr 2025 21:45:21 +0100 Message-ID: <20250423204531.2432180-4-sw@jkqxz.net> (raw) In-Reply-To: <20250423204531.2432180-1-sw@jkqxz.net> Demuxes raw streams as defined in draft spec section 10.2. --- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/apvdec.c | 241 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 243 insertions(+) create mode 100644 libavformat/apvdec.c diff --git a/libavformat/Makefile b/libavformat/Makefile index a94ac66e7e..ef96c2762e 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -119,6 +119,7 @@ OBJS-$(CONFIG_APTX_DEMUXER) += aptxdec.o OBJS-$(CONFIG_APTX_MUXER) += rawenc.o OBJS-$(CONFIG_APTX_HD_DEMUXER) += aptxdec.o OBJS-$(CONFIG_APTX_HD_MUXER) += rawenc.o +OBJS-$(CONFIG_APV_DEMUXER) += apvdec.o OBJS-$(CONFIG_AQTITLE_DEMUXER) += aqtitledec.o subtitles.o OBJS-$(CONFIG_ARGO_ASF_DEMUXER) += argo_asf.o OBJS-$(CONFIG_ARGO_ASF_MUXER) += argo_asf.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 445f13f42a..90a4fe64ec 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -72,6 +72,7 @@ extern const FFInputFormat ff_aptx_demuxer; extern const FFOutputFormat ff_aptx_muxer; extern const FFInputFormat ff_aptx_hd_demuxer; extern const FFOutputFormat ff_aptx_hd_muxer; +extern const FFInputFormat ff_apv_demuxer; extern const FFInputFormat ff_aqtitle_demuxer; extern const FFInputFormat ff_argo_asf_demuxer; extern const FFOutputFormat ff_argo_asf_muxer; diff --git a/libavformat/apvdec.c b/libavformat/apvdec.c new file mode 100644 index 0000000000..04f9ef0a8f --- /dev/null +++ b/libavformat/apvdec.c @@ -0,0 +1,241 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavcodec/apv.h" +#include "libavcodec/bytestream.h" + +#include "avformat.h" +#include "avio_internal.h" +#include "demux.h" +#include "internal.h" + + +typedef struct APVHeaderInfo { + uint8_t pbu_type; + uint16_t group_id; + + uint8_t profile_idc; + uint8_t level_idc; + uint8_t band_idc; + + int frame_width; + int frame_height; + + uint8_t chroma_format_idc; + uint8_t bit_depth_minus8; + + enum AVPixelFormat pixel_format; +} APVHeaderInfo; + +static const enum AVPixelFormat apv_format_table[5][5] = { + { AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16 }, + { 0 }, // 4:2:0 is not valid. + { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_YUV422P16 }, + { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_YUV444P16 }, + { AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_YUVA444P16 }, +}; + +static int apv_extract_header_info(APVHeaderInfo *info, + GetByteContext *gbc) +{ + int zero, byte, bit_depth_index; + + info->pbu_type = bytestream2_get_byte(gbc); + info->group_id = bytestream2_get_be16(gbc); + + zero = bytestream2_get_byte(gbc); + if (zero != 0) + return AVERROR_INVALIDDATA; + + if (info->pbu_type != APV_PBU_PRIMARY_FRAME) + return AVERROR_INVALIDDATA; + + info->profile_idc = bytestream2_get_byte(gbc); + info->level_idc = bytestream2_get_byte(gbc); + + byte = bytestream2_get_byte(gbc); + info->band_idc = byte >> 3; + zero = byte & 7; + if (zero != 0) + return AVERROR_INVALIDDATA; + + info->frame_width = bytestream2_get_be24(gbc); + info->frame_height = bytestream2_get_be24(gbc); + if (info->frame_width < 1 || info->frame_width > 65536 || + info->frame_height < 1 || info->frame_height > 65536) + return AVERROR_INVALIDDATA; + + byte = bytestream2_get_byte(gbc); + info->chroma_format_idc = byte >> 4; + info->bit_depth_minus8 = byte & 0xf; + + if (info->bit_depth_minus8 > 8) { + return AVERROR_INVALIDDATA; + } + if (info->bit_depth_minus8 % 2) { + // Odd bit depths are technically valid but not useful here. + return AVERROR_INVALIDDATA; + } + bit_depth_index = info->bit_depth_minus8 / 2; + + switch (info->chroma_format_idc) { + case APV_CHROMA_FORMAT_400: + case APV_CHROMA_FORMAT_422: + case APV_CHROMA_FORMAT_444: + case APV_CHROMA_FORMAT_4444: + info->pixel_format = apv_format_table[info->chroma_format_idc][bit_depth_index]; + break; + default: + return AVERROR_INVALIDDATA; + } + + // Ignore capture_time_distance. + bytestream2_skip(gbc, 1); + + zero = bytestream2_get_byte(gbc); + if (zero != 0) + return AVERROR_INVALIDDATA; + + return 1; +} + +static int apv_probe(const AVProbeData *p) +{ + GetByteContext gbc; + APVHeaderInfo header; + uint32_t au_size, signature, pbu_size; + int err; + + if (p->buf_size < 28) { + // Too small to fit an APV header. + return 0; + } + + bytestream2_init(&gbc, p->buf, p->buf_size); + + au_size = bytestream2_get_be32(&gbc); + if (au_size < 24) { + // Too small. + return 0; + } + signature = bytestream2_get_be32(&gbc); + if (signature != APV_SIGNATURE) { + // Signature is mandatory. + return 0; + } + pbu_size = bytestream2_get_be32(&gbc); + if (pbu_size < 16) { + // Too small. + return 0; + } + + err = apv_extract_header_info(&header, &gbc); + if (err < 0) { + // Header does not look like APV. + return 0; + } + return AVPROBE_SCORE_MAX; +} + +static int apv_read_header(AVFormatContext *s) +{ + AVStream *st; + GetByteContext gbc; + APVHeaderInfo header; + uint8_t buffer[28]; + uint32_t au_size, signature, pbu_size; + int err, size; + + err = ffio_ensure_seekback(s->pb, sizeof(buffer)); + if (err < 0) + return err; + size = avio_read(s->pb, buffer, sizeof(buffer)); + if (size < 0) + return size; + + bytestream2_init(&gbc, buffer, sizeof(buffer)); + + au_size = bytestream2_get_be32(&gbc); + if (au_size < 24) { + // Too small. + return AVERROR_INVALIDDATA; + } + signature = bytestream2_get_be32(&gbc); + if (signature != APV_SIGNATURE) { + // Signature is mandatory. + return AVERROR_INVALIDDATA; + } + pbu_size = bytestream2_get_be32(&gbc); + if (pbu_size < 16) { + // Too small. + return AVERROR_INVALIDDATA; + } + + err = apv_extract_header_info(&header, &gbc); + if (err < 0) + return err; + + st = avformat_new_stream(s, NULL); + if (!st) + return AVERROR(ENOMEM); + + st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; + st->codecpar->codec_id = AV_CODEC_ID_APV; + st->codecpar->format = header.pixel_format; + st->codecpar->profile = header.profile_idc; + st->codecpar->level = header.level_idc; + st->codecpar->width = header.frame_width; + st->codecpar->height = header.frame_height; + + st->avg_frame_rate = (AVRational){ 30, 1 }; + avpriv_set_pts_info(st, 64, 1, 30); + + avio_seek(s->pb, -size, SEEK_CUR); + + return 0; +} + +static int apv_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + uint32_t au_size; + int ret; + + au_size = avio_rb32(s->pb); + if (au_size == 0 && avio_feof(s->pb)) + return AVERROR_EOF; + if (au_size < 16 || au_size > 1 << 24) { + av_log(s, AV_LOG_ERROR, "APV AU is bad\n"); + return AVERROR_INVALIDDATA; + } + + ret = av_get_packet(s->pb, pkt, au_size); + pkt->flags = AV_PKT_FLAG_KEY; + + return ret; +} + +const FFInputFormat ff_apv_demuxer = { + .p.name = "apv", + .p.long_name = NULL_IF_CONFIG_SMALL("APV raw bitstream"), + .p.extensions = "apv", + .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NOTIMESTAMPS, + .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, + .read_probe = apv_probe, + .read_header = apv_read_header, + .read_packet = apv_read_packet, +}; -- 2.47.2 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2025-04-23 20:46 UTC|newest] Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top 2025-04-23 20:45 [FFmpeg-devel] [PATCH v3 0/7] APV support Mark Thompson 2025-04-23 20:45 ` [FFmpeg-devel] [PATCH v3 1/7] lavc: APV codec ID and descriptor Mark Thompson 2025-04-23 20:45 ` [FFmpeg-devel] [PATCH v3 2/7] lavc/cbs: APV support Mark Thompson 2025-04-24 0:02 ` James Almer 2025-04-24 20:16 ` Mark Thompson 2025-04-23 20:45 ` Mark Thompson [this message] 2025-04-24 0:10 ` [FFmpeg-devel] [PATCH v3 3/7] lavf: APV demuxer James Almer 2025-04-24 20:15 ` Mark Thompson 2025-04-23 20:45 ` [FFmpeg-devel] [PATCH v3 4/7] lavc: APV decoder Mark Thompson 2025-04-24 3:04 ` James Almer 2025-04-25 17:25 ` Michael Niedermayer 2025-04-23 20:45 ` [FFmpeg-devel] [PATCH v3 5/7] lavc/apv: AVX2 transquant for x86-64 Mark Thompson 2025-04-24 2:55 ` James Almer 2025-04-24 20:37 ` Mark Thompson 2025-04-24 21:41 ` James Almer 2025-04-23 20:45 ` [FFmpeg-devel] [PATCH v3 6/7] lavc: APV metadata bitstream filter Mark Thompson 2025-04-23 20:45 ` [FFmpeg-devel] [PATCH v3 7/7] lavf: APV muxer Mark Thompson
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20250423204531.2432180-4-sw@jkqxz.net \ --to=sw@jkqxz.net \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git