FFmpeg
Loading...
Searching...
No Matches
apv_parser.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 "libavutil/avassert.h"
20#include "libavutil/buffer.h"
21#include "libavutil/mem.h"
22
23#include "avcodec.h"
24#include "apv.h"
25#include "cbs.h"
26#include "cbs_apv.h"
27#include "parser.h"
28#include "parser_internal.h"
29
36
44
45static int find_frame_end(APVParseContext *p, const uint8_t *buf, int buf_size)
46{
47 ParseContext *pc = &p->pc;
48 int pic_found, i = 0;
49 uint32_t state;
50
51 pic_found = pc->frame_start_found;
52 state = pc->state;
53
54 if (buf_size == 0) {
55 pc->frame_start_found = 0;
56 pc->state = -1;
57 return 0;
58 }
59
60 if (!pic_found) {
61 for (; i < buf_size; i++) {
62 state = (state << 8) | buf[i];
63 if (state == APV_SIGNATURE) {
64 i++;
65 pic_found = 1;
66 break;
67 }
68 }
69 }
70
71 if (pic_found) {
72 for(; i < buf_size; i++) {
73 state = (state << 8) | buf[i];
74 if (state == APV_SIGNATURE) {
75 pc->frame_start_found = 0;
76 pc->state = -1;
77 return i - 3;
78 }
79 }
80 }
81
82 pc->frame_start_found = pic_found;
83 pc->state = state;
84 return END_NOT_FOUND;
85}
86
87static void dummy_free(void *opaque, uint8_t *data)
88{
89 av_assert0(opaque == data);
90}
91
93 AVCodecContext *avctx,
94 const uint8_t **poutbuf, int *poutbuf_size,
95 const uint8_t *buf, int buf_size)
96{
97 APVParseContext *p = s->priv_data;
98 CodedBitstreamFragment *au = &p->au;
100 int next, ret;
101
102 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
103 next = buf_size;
104 } else {
105 next = find_frame_end(p, buf, buf_size);
106
107 if (ff_combine_frame(&p->pc, next, &buf, &buf_size) < 0) {
108 *poutbuf = NULL;
109 *poutbuf_size = 0;
110 return buf_size;
111 }
112 }
113
114 *poutbuf = buf;
115 *poutbuf_size = buf_size;
116
117 if (!buf_size)
118 return 0;
119
120 ref = av_buffer_create((uint8_t *)buf, buf_size, dummy_free,
121 (void *)buf, AV_BUFFER_FLAG_READONLY);
122 if (!ref)
123 return next;
124
125 p->cbc->log_ctx = avctx;
126
127 ret = ff_cbs_read(p->cbc, au, ref, buf, buf_size);
128 if (ret < 0) {
129 av_log(avctx, AV_LOG_ERROR, "Failed to parse access unit.\n");
130 goto end;
131 }
132
133 s->key_frame = 1;
134 s->pict_type = AV_PICTURE_TYPE_I;
135 s->field_order = AV_FIELD_UNKNOWN;
136 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
137
138 for (int i = 0; i < au->nb_units; i++) {
139 const CodedBitstreamUnit *pbu = &au->units[i];
140
141 switch (pbu->type) {
143 const APVRawFrame *frame = pbu->content;
144 const APVRawFrameHeader *header = &frame->frame_header;
145 const APVRawFrameInfo *info = &header->frame_info;
146 int bit_depth = info->bit_depth_minus8 + 8;
147
149 break;
150
151 s->width = info->frame_width;
152 s->height = info->frame_height;
153 s->format = apv_format_table[info->chroma_format_idc][bit_depth - 4 >> 2];
154 avctx->profile = info->profile_idc;
155 avctx->level = info->level_idc;
157 avctx->color_primaries = header->color_primaries;
158 avctx->color_trc = header->transfer_characteristics;
159 avctx->colorspace = header->matrix_coefficients;
160 avctx->color_range = header->full_range_flag ? AVCOL_RANGE_JPEG
162 goto end;
163 }
164 default:
165 break;
166 }
167 }
168
169end:
170 ff_cbs_fragment_reset(au);
173 p->cbc->log_ctx = NULL;
174
175 return next;
176}
177
181
183{
184 APVParseContext *p = s->priv_data;
185 int ret;
186
187 ret = ff_cbs_init(&p->cbc, AV_CODEC_ID_APV, NULL);
188 if (ret < 0)
189 return ret;
190
191 p->cbc->decompose_unit_types = decompose_unit_types;
192 p->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
193
194 return 0;
195}
196
198{
199 APVParseContext *p = s->priv_data;
200 ParseContext *pc = &p->pc;
201
202 av_freep(&pc->buffer);
203 ff_cbs_fragment_free(&p->au);
204 ff_cbs_close(&p->cbc);
205}
206
209 .priv_data_size = sizeof(APVParseContext),
210 .init = init,
211 .parse = parse,
212 .close = close,
213};
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
Definition af_astats.c:246
static enum AVPixelFormat apv_format_table[5][4]
Definition apv_decode.c:40
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition apv_parser.c:92
static void dummy_free(void *opaque, uint8_t *data)
Definition apv_parser.c:87
static const CodedBitstreamUnitType decompose_unit_types[]
Definition apv_parser.c:178
static int find_frame_end(APVParseContext *p, const uint8_t *buf, int buf_size)
Definition apv_parser.c:45
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
const FFCodecParser ff_apv_parser
Definition apv_parser.c:207
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2651
refcounted data buffer API
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
@ AV_FIELD_UNKNOWN
Definition defs.h:212
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static struct @346255127015250356166251341105367306144006377143 state
@ AV_CODEC_ID_APV
Definition codec_id.h:323
@ AV_PICTURE_STRUCTURE_FRAME
coded as frame
Definition avcodec.h:2614
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:139
int av_buffer_get_ref_count(const AVBufferRef *buf)
Definition buffer.c:160
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition buffer.h:114
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition buffer.c:55
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ APV_PBU_PRIMARY_FRAME
Definition apv.h:27
#define APV_SIGNATURE
Definition apv.h:23
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition cbs.h:66
#define av_cold
Definition attributes.h:117
Memory handling functions.
const char data[16]
Definition mxf.c:149
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition parser.c:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
@ AVCHROMA_LOC_TOPLEFT
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition pixfmt.h:806
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:783
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:598
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:526
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:525
#define AV_PIX_FMT_GRAY14
Definition pixfmt.h:527
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:557
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:528
#define AV_PIX_FMT_YUVA444P16
Definition pixfmt.h:603
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:600
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:558
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
static const uint8_t header[24]
Definition sdr2.c:68
#define FF_ARRAY_ELEMS(a)
CodedBitstreamContext * cbc
Definition apv_parser.c:33
CodedBitstreamFragment au
Definition apv_parser.c:34
ParseContext pc
Definition apv_parser.c:31
uint8_t level_idc
Definition cbs_apv.h:45
uint32_t frame_width
Definition cbs_apv.h:48
uint8_t bit_depth_minus8
Definition cbs_apv.h:51
uint8_t profile_idc
Definition cbs_apv.h:44
uint32_t frame_height
Definition cbs_apv.h:49
uint8_t chroma_format_idc
Definition cbs_apv.h:50
A reference to a data buffer.
Definition buffer.h:82
main external API structure.
Definition avcodec.h:443
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition avcodec.h:681
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition avcodec.h:657
int level
Encoding level descriptor.
Definition avcodec.h:1646
int profile
profile
Definition avcodec.h:1636
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:671
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:664
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition avcodec.h:688
Context structure for coded bitstream operations.
Definition cbs.h:226
Coded bitstream fragment structure, combining one or more units.
Definition cbs.h:129
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition cbs.h:175
int nb_units
Number of units in this fragment.
Definition cbs.h:160
Coded bitstream unit structure.
Definition cbs.h:77
void * content
Pointer to the decomposed form of this unit.
Definition cbs.h:114
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition cbs.h:81
uint32_t state
contains the last few bytes in MSB order
Definition parser.h:33
int frame_start_found
Definition parser.h:34
uint8_t * buffer
Definition parser.h:29
#define av_freep(p)
#define av_log(a,...)
static int ref[MAX_W *MAX_W]