FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
22 #include "avcodec.h"
23 #include "apv.h"
24 #include "cbs.h"
25 #include "cbs_apv.h"
26 
27 typedef struct APVParseContext {
31 
32 static const enum AVPixelFormat apv_format_table[5][5] = {
34  { 0 }, // 4:2:0 is not valid.
38 };
39 
40 static void dummy_free(void *opaque, uint8_t *data)
41 {
42  av_assert0(opaque == data);
43 }
44 
46  AVCodecContext *avctx,
47  const uint8_t **poutbuf, int *poutbuf_size,
48  const uint8_t *buf, int buf_size)
49 {
50  APVParseContext *p = s->priv_data;
51  CodedBitstreamFragment *au = &p->au;
52  AVBufferRef *ref = NULL;
53  int ret;
54 
55  *poutbuf = buf;
56  *poutbuf_size = buf_size;
57 
58  ref = av_buffer_create((uint8_t *)buf, buf_size, dummy_free,
59  (void *)buf, AV_BUFFER_FLAG_READONLY);
60  if (!ref)
61  return buf_size;
62 
63  p->cbc->log_ctx = avctx;
64 
65  ret = ff_cbs_read(p->cbc, au, ref, buf, buf_size);
66  if (ret < 0) {
67  av_log(avctx, AV_LOG_ERROR, "Failed to parse access unit.\n");
68  goto end;
69  }
70 
71  s->key_frame = 1;
72  s->pict_type = AV_PICTURE_TYPE_I;
73  s->field_order = AV_FIELD_UNKNOWN;
74  s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
75 
76  for (int i = 0; i < au->nb_units; i++) {
77  const CodedBitstreamUnit *pbu = &au->units[i];
78 
79  switch (pbu->type) {
80  case APV_PBU_PRIMARY_FRAME: {
81  const APVRawFrame *frame = pbu->content;
82  const APVRawFrameHeader *header = &frame->frame_header;
83  const APVRawFrameInfo *info = &header->frame_info;
84  int bit_depth = info->bit_depth_minus8 + 8;
85 
86  if (bit_depth < 8 || bit_depth > 16 || bit_depth % 2)
87  break;
88 
89  s->width = info->frame_width;
90  s->height = info->frame_height;
91  s->format = apv_format_table[info->chroma_format_idc][bit_depth - 4 >> 2];
92  avctx->profile = info->profile_idc;
93  avctx->level = info->level_idc;
95  avctx->color_primaries = header->color_primaries;
96  avctx->color_trc = header->transfer_characteristics;
97  avctx->colorspace = header->matrix_coefficients;
98  avctx->color_range = header->full_range_flag ? AVCOL_RANGE_JPEG
100  goto end;
101  }
102  default:
103  break;
104  }
105  }
106 
107 end:
108  ff_cbs_fragment_reset(au);
111  p->cbc->log_ctx = NULL;
112 
113  return buf_size;
114 }
115 
118 };
119 
121 {
122  APVParseContext *p = s->priv_data;
123  int ret;
124 
125  ret = ff_cbs_init(&p->cbc, AV_CODEC_ID_APV, NULL);
126  if (ret < 0)
127  return ret;
128 
131 
132  return 0;
133 }
134 
136 {
137  APVParseContext *p = s->priv_data;
138 
139  ff_cbs_fragment_free(&p->au);
140  ff_cbs_close(&p->cbc);
141 }
142 
144  .codec_ids = { AV_CODEC_ID_APV },
145  .priv_data_size = sizeof(APVParseContext),
146  .parser_init = init,
147  .parser_parse = parse,
148  .parser_close = close,
149 };
cbs.h
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:659
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:114
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:652
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:750
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:226
data
const char data[16]
Definition: mxf.c:149
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:81
ff_apv_parser
const AVCodecParser ff_apv_parser
Definition: apv_parser.c:143
bit_depth
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
Definition: af_astats.c:246
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:77
CodedBitstreamContext::log_ctx
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:231
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:135
AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:580
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:511
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:531
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:175
avassert.h
AV_PICTURE_STRUCTURE_FRAME
@ AV_PICTURE_STRUCTURE_FRAME
coded as frame
Definition: avcodec.h:2564
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:645
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
decompose_unit_types
static const CodedBitstreamUnitType decompose_unit_types[]
Definition: apv_parser.c:116
AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:540
AV_FIELD_UNKNOWN
@ AV_FIELD_UNKNOWN
Definition: defs.h:209
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:129
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:541
AV_BUFFER_FLAG_READONLY
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition: buffer.h:114
APVRawFrameInfo
Definition: cbs_apv.h:43
info
MIPS optimizations info
Definition: mips.txt:2
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
CodedBitstreamUnitType
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:54
AV_PIX_FMT_YUVA444P12
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:577
init
static av_cold int init(AVCodecParserContext *s)
Definition: apv_parser.c:120
AV_PIX_FMT_GRAY14
#define AV_PIX_FMT_GRAY14
Definition: pixfmt.h:510
APVParseContext::au
CodedBitstreamFragment au
Definition: apv_parser.c:29
APVParseContext::cbc
CodedBitstreamContext * cbc
Definition: apv_parser.c:28
AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:508
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:669
av_buffer_unref
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
AVCHROMA_LOC_TOPLEFT
@ AVCHROMA_LOC_TOPLEFT
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:773
apv_format_table
static enum AVPixelFormat apv_format_table[5][5]
Definition: apv_parser.c:32
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:278
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
parse
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:45
AVCodecContext::level
int level
Encoding level descriptor.
Definition: avcodec.h:1628
APVRawFrame
Definition: cbs_apv.h:101
av_buffer_create
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
dummy_free
static void dummy_free(void *opaque, uint8_t *data)
Definition: apv_parser.c:40
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2727
APVRawFrameHeader
Definition: cbs_apv.h:67
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:533
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:535
apv.h
header
static const uint8_t header[24]
Definition: sdr2.c:68
buffer.h
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
AV_PIX_FMT_YUVA444P10
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:575
av_buffer_get_ref_count
int av_buffer_get_ref_count(const AVBufferRef *buf)
Definition: buffer.c:160
APVParseContext
Definition: apv_parser.c:27
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:676
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:733
avcodec.h
AVCodecParserContext
Definition: avcodec.h:2567
ret
ret
Definition: filter_design.txt:187
AV_CODEC_ID_APV
@ AV_CODEC_ID_APV
Definition: codec_id.h:332
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:431
APV_PBU_PRIMARY_FRAME
@ APV_PBU_PRIMARY_FRAME
Definition: apv.h:27
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1618
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:117
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
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
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
CodedBitstreamContext::nb_decompose_unit_types
int nb_decompose_unit_types
Length of the decompose_unit_types array.
Definition: cbs.h:259
CodedBitstreamContext::decompose_unit_types
const CodedBitstreamUnitType * decompose_unit_types
Array of unit types which should be decomposed when reading.
Definition: cbs.h:255
AVCodecParser
Definition: avcodec.h:2726
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:509
cbs_apv.h
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:160