FFmpeg
apvdec.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavcodec/apv.h"
20 #include "libavcodec/bytestream.h"
21 
22 #include "avformat.h"
23 #include "avio_internal.h"
24 #include "demux.h"
25 #include "internal.h"
26 
27 
28 typedef struct APVHeaderInfo {
29  uint8_t pbu_type;
30  uint16_t group_id;
31 
32  uint8_t profile_idc;
33  uint8_t level_idc;
34  uint8_t band_idc;
35 
38 
41 
44 
45 static const enum AVPixelFormat apv_format_table[5][5] = {
47  { 0 }, // 4:2:0 is not valid.
51 };
52 
54  GetByteContext *gbc)
55 {
56  int zero, byte, bit_depth_index;
57 
58  info->pbu_type = bytestream2_get_byte(gbc);
59  info->group_id = bytestream2_get_be16(gbc);
60 
61  zero = bytestream2_get_byte(gbc);
62  if (zero != 0)
63  return AVERROR_INVALIDDATA;
64 
65  if (info->pbu_type != APV_PBU_PRIMARY_FRAME)
66  return AVERROR_INVALIDDATA;
67 
68  info->profile_idc = bytestream2_get_byte(gbc);
69  info->level_idc = bytestream2_get_byte(gbc);
70 
71  byte = bytestream2_get_byte(gbc);
72  info->band_idc = byte >> 3;
73  zero = byte & 7;
74  if (zero != 0)
75  return AVERROR_INVALIDDATA;
76 
77  info->frame_width = bytestream2_get_be24(gbc);
78  info->frame_height = bytestream2_get_be24(gbc);
79  if (info->frame_width < 1 || info->frame_width > 65536 ||
80  info->frame_height < 1 || info->frame_height > 65536)
81  return AVERROR_INVALIDDATA;
82 
83  byte = bytestream2_get_byte(gbc);
84  info->chroma_format_idc = byte >> 4;
85  info->bit_depth_minus8 = byte & 0xf;
86 
87  if (info->bit_depth_minus8 > 8) {
88  return AVERROR_INVALIDDATA;
89  }
90  if (info->bit_depth_minus8 % 2) {
91  // Odd bit depths are technically valid but not useful here.
92  return AVERROR_INVALIDDATA;
93  }
94  bit_depth_index = info->bit_depth_minus8 / 2;
95 
96  switch (info->chroma_format_idc) {
101  info->pixel_format = apv_format_table[info->chroma_format_idc][bit_depth_index];
102  break;
103  default:
104  return AVERROR_INVALIDDATA;
105  }
106 
107  // Ignore capture_time_distance.
108  bytestream2_skip(gbc, 1);
109 
110  zero = bytestream2_get_byte(gbc);
111  if (zero != 0)
112  return AVERROR_INVALIDDATA;
113 
114  return 1;
115 }
116 
117 static int apv_probe(const AVProbeData *p)
118 {
119  GetByteContext gbc;
121  uint32_t au_size, signature, pbu_size;
122  int err;
123 
124  if (p->buf_size < 28) {
125  // Too small to fit an APV header.
126  return 0;
127  }
128 
129  bytestream2_init(&gbc, p->buf, p->buf_size);
130 
131  au_size = bytestream2_get_be32(&gbc);
132  if (au_size < 24) {
133  // Too small.
134  return 0;
135  }
136  signature = bytestream2_get_be32(&gbc);
137  if (signature != APV_SIGNATURE) {
138  // Signature is mandatory.
139  return 0;
140  }
141  pbu_size = bytestream2_get_be32(&gbc);
142  if (pbu_size < 16) {
143  // Too small.
144  return 0;
145  }
146 
147  err = apv_extract_header_info(&header, &gbc);
148  if (err < 0) {
149  // Header does not look like APV.
150  return 0;
151  }
152  return AVPROBE_SCORE_MAX;
153 }
154 
156 {
157  AVStream *st;
158  GetByteContext gbc;
160  uint8_t buffer[28];
161  uint32_t au_size, signature, pbu_size;
162  int err, size;
163 
164  err = ffio_ensure_seekback(s->pb, sizeof(buffer));
165  if (err < 0)
166  return err;
167  size = avio_read(s->pb, buffer, sizeof(buffer));
168  if (size < 0)
169  return size;
170 
171  bytestream2_init(&gbc, buffer, sizeof(buffer));
172 
173  au_size = bytestream2_get_be32(&gbc);
174  if (au_size < 24) {
175  // Too small.
176  return AVERROR_INVALIDDATA;
177  }
178  signature = bytestream2_get_be32(&gbc);
179  if (signature != APV_SIGNATURE) {
180  // Signature is mandatory.
181  return AVERROR_INVALIDDATA;
182  }
183  pbu_size = bytestream2_get_be32(&gbc);
184  if (pbu_size < 16) {
185  // Too small.
186  return AVERROR_INVALIDDATA;
187  }
188 
189  err = apv_extract_header_info(&header, &gbc);
190  if (err < 0)
191  return err;
192 
193  st = avformat_new_stream(s, NULL);
194  if (!st)
195  return AVERROR(ENOMEM);
196 
199  st->codecpar->format = header.pixel_format;
200  st->codecpar->profile = header.profile_idc;
201  st->codecpar->level = header.level_idc;
202  st->codecpar->width = header.frame_width;
203  st->codecpar->height = header.frame_height;
204 
205  st->avg_frame_rate = (AVRational){ 30, 1 };
206  avpriv_set_pts_info(st, 64, 1, 30);
207 
208  avio_seek(s->pb, -size, SEEK_CUR);
209 
210  return 0;
211 }
212 
214 {
215  uint32_t au_size, signature;
216  int ret;
217 
218  au_size = avio_rb32(s->pb);
219  if (au_size == 0 && avio_feof(s->pb))
220  return AVERROR_EOF;
221  if (au_size < 24 || au_size > 1 << 24) {
223  "APV AU has invalid size: %"PRIu32"\n", au_size);
224  return AVERROR_INVALIDDATA;
225  }
226 
227  ret = av_get_packet(s->pb, pkt, au_size);
229 
231  if (signature != APV_SIGNATURE) {
232  av_log(s, AV_LOG_ERROR, "APV AU has invalid signature.\n");
233  return AVERROR_INVALIDDATA;
234  }
235 
236  return ret;
237 }
238 
240  .p.name = "apv",
241  .p.long_name = NULL_IF_CONFIG_SMALL("APV raw bitstream"),
242  .p.extensions = "apv",
244  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
245  .read_probe = apv_probe,
246  .read_header = apv_read_header,
247  .read_packet = apv_read_packet,
248 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
apv_format_table
static enum AVPixelFormat apv_format_table[5][5]
Definition: apvdec.c:45
GetByteContext
Definition: bytestream.h:33
APV_CHROMA_FORMAT_444
@ APV_CHROMA_FORMAT_444
Definition: apv.h:49
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
APVHeaderInfo::band_idc
uint8_t band_idc
Definition: apvdec.c:34
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
APV_SIGNATURE
#define APV_SIGNATURE
Definition: apv.h:23
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:479
APVHeaderInfo::group_id
uint16_t group_id
Definition: apvdec.c:30
APVHeaderInfo::frame_height
int frame_height
Definition: apvdec.c:37
AVPacket::data
uint8_t * data
Definition: packet.h:535
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:834
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:590
APV_CHROMA_FORMAT_400
@ APV_CHROMA_FORMAT_400
Definition: apv.h:47
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:777
AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:580
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:511
APVHeaderInfo::pixel_format
enum AVPixelFormat pixel_format
Definition: apvdec.c:42
signature
static const char signature[]
Definition: ipmovie.c:592
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:531
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:761
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:540
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:541
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:550
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
info
MIPS optimizations info
Definition: mips.txt:2
AV_PIX_FMT_YUVA444P12
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:577
AV_PIX_FMT_GRAY14
#define AV_PIX_FMT_GRAY14
Definition: pixfmt.h:510
APVHeaderInfo::bit_depth_minus8
uint8_t bit_depth_minus8
Definition: apvdec.c:40
apv_read_packet
static int apv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: apvdec.c:213
AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:508
FF_INFMT_FLAG_INIT_CLEANUP
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition: demux.h:35
AVFormatContext
Format I/O context.
Definition: avformat.h:1265
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:768
NULL
#define NULL
Definition: coverity.c:32
apv_probe
static int apv_probe(const AVProbeData *p)
Definition: apvdec.c:117
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
APVHeaderInfo::frame_width
int frame_width
Definition: apvdec.c:36
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:529
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
AVCodecParameters::level
int level
Definition: codec_par.h:129
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
byte
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition: bytestream.h:99
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:533
size
int size
Definition: twinvq_data.h:10344
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:535
AVCodecParameters::profile
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: codec_par.h:128
apv.h
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
header
static const uint8_t header[24]
Definition: sdr2.c:68
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:174
ffio_ensure_seekback
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:1023
AV_PIX_FMT_YUVA444P10
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:575
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:541
zero
static int zero(InterplayACMContext *s, unsigned ind, unsigned col)
Definition: interplayacm.c:121
avio_internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:135
APVHeaderInfo::pbu_type
uint8_t pbu_type
Definition: apvdec.c:29
apv_extract_header_info
static int apv_extract_header_info(APVHeaderInfo *info, GetByteContext *gbc)
Definition: apvdec.c:53
APVHeaderInfo::chroma_format_idc
uint8_t chroma_format_idc
Definition: apvdec.c:39
demux.h
APVHeaderInfo::level_idc
uint8_t level_idc
Definition: apvdec.c:33
APV_CHROMA_FORMAT_422
@ APV_CHROMA_FORMAT_422
Definition: apv.h:48
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:94
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:745
AV_CODEC_ID_APV
@ AV_CODEC_ID_APV
Definition: codec_id.h:332
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
avformat.h
APV_PBU_PRIMARY_FRAME
@ APV_PBU_PRIMARY_FRAME
Definition: apv.h:27
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
ff_apv_demuxer
const FFInputFormat ff_apv_demuxer
Definition: apvdec.c:239
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
APV_CHROMA_FORMAT_4444
@ APV_CHROMA_FORMAT_4444
Definition: apv.h:50
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
AVCodecParameters::format
int format
Definition: codec_par.h:92
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:512
apv_read_header
static int apv_read_header(AVFormatContext *s)
Definition: apvdec.c:155
FFInputFormat
Definition: demux.h:42
bytestream.h
APVHeaderInfo
Definition: apvdec.c:28
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
APVHeaderInfo::profile_idc
uint8_t profile_idc
Definition: apvdec.c:32
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:509
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346