FFmpeg
Loading...
Searching...
No Matches
lcevc_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 <stdint.h>
20
22#include "libavutil/mem.h"
23
24#include "avcodec.h"
25#include "bytestream.h"
26#include "get_bits.h"
27#include "h2645_parse.h"
28#include "lcevc.h"
29#include "lcevc_parse.h"
30#include "lcevctab.h"
31#include "parser.h"
32#include "parser_internal.h"
33
34#define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
35
45
46static int lcevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
47 int buf_size)
48{
49 LCEVCParserContext *ctx = s->priv_data;
50 ParseContext *pc = &ctx->pc;
51
52 for (int i = 0; i < buf_size; i++) {
53 int nut;
54
55 pc->state = (pc->state << 8) | buf[i];
56
57 if (((pc->state >> 8) & 0xFFFFFF) != START_CODE)
58 continue;
59
60 nut = (pc->state >> 1) & 0x1F;
61
62 // Beginning of access unit
63 if (nut == LCEVC_IDR_NUT || nut == LCEVC_NON_IDR_NUT) {
64 if (!pc->frame_start_found)
65 pc->frame_start_found = 1;
66 else {
67 pc->frame_start_found = 0;
68 return i - 3;
69 }
70 }
71 }
72
73 return END_NOT_FOUND;
74}
75
77 const H2645NAL *nal)
78{
80 bytestream2_init(&gbc, nal->data, nal->size);
81 bytestream2_skip(&gbc, 2);
82
83 while (bytestream2_get_bytes_left(&gbc) > 1) {
85 uint64_t payload_size;
86 int payload_size_type, payload_type;
87 int block_size;
88
89 int ret = init_get_bits8(&gb, gbc.buffer, bytestream2_get_bytes_left(&gbc));
90 if (ret < 0)
91 return ret;
92
93 payload_size_type = get_bits(&gb, 3);
94 payload_type = get_bits(&gb, 5);
95 payload_size = payload_size_type;
96 if (payload_size_type == 6)
98 if (payload_size_type == 7)
99 payload_size = get_mb(&gb);
100
101 if (payload_size > INT_MAX - (get_bits_count(&gb) >> 3))
102 return AVERROR_INVALIDDATA;
103
104 block_size = payload_size + (get_bits_count(&gb) >> 3);
105 if (block_size >= bytestream2_get_bytes_left(&gbc))
106 return AVERROR_INVALIDDATA;
107
108 switch (payload_type) {
110 avctx->profile = get_bits(&gb, 4);
111 avctx->level = get_bits(&gb, 4);
112 break;
114 int resolution_type, chroma_format_idc, bit_depth;
115 int processed_planes_type_flag;
116
117 processed_planes_type_flag = get_bits1(&gb);
118 resolution_type = get_bits(&gb, 6);
119 skip_bits1(&gb);
120 chroma_format_idc = get_bits(&gb, 2);
121 skip_bits(&gb, 2);
122 bit_depth = get_bits(&gb, 2); // enhancement_depth_type
123
124 s->format = ff_lcevc_depth_type[bit_depth][chroma_format_idc];
125
126 if (resolution_type < 63) {
127 s->width = ff_lcevc_resolution_type[resolution_type].width;
128 s->height = ff_lcevc_resolution_type[resolution_type].height;
129 } else {
130 int upsample_type, tile_dimensions_type;
131 int temporal_step_width_modifier_signalled_flag, level1_filtering_signalled_flag;
132 // Skip syntax elements until we get to the custom dimension ones
133 temporal_step_width_modifier_signalled_flag = get_bits1(&gb);
134 skip_bits(&gb, 3);
135 upsample_type = get_bits(&gb, 3);
136 level1_filtering_signalled_flag = get_bits1(&gb);
137 skip_bits(&gb, 4);
138 tile_dimensions_type = get_bits(&gb, 2);
139 skip_bits(&gb, 4);
140 if (processed_planes_type_flag)
141 skip_bits(&gb, 4);
142 if (temporal_step_width_modifier_signalled_flag)
143 skip_bits(&gb, 8);
144 if (upsample_type)
145 skip_bits_long(&gb, 64);
146 if (level1_filtering_signalled_flag)
147 skip_bits(&gb, 8);
148 if (tile_dimensions_type) {
149 if (tile_dimensions_type == 3)
150 skip_bits_long(&gb, 32);
151 skip_bits(&gb, 8);
152 }
153
154 s->width = get_bits(&gb, 16);
155 s->height = get_bits(&gb, 16);
156 }
157 break;
158 }
159 default:
160 break;
161 }
162
163 bytestream2_skip(&gbc, block_size);
164 }
165
166 return 0;
167}
168
169static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
170 int buf_size, AVCodecContext *avctx)
171{
172 LCEVCParserContext *ctx = s->priv_data;
174 int ret, i;
175
176 /* set some sane default values */
177 s->pict_type = AV_PICTURE_TYPE_NONE;
178 s->key_frame = 0;
179 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
180
181 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx,
182 ctx->nal_length_size, AV_CODEC_ID_LCEVC, flags);
183 if (ret < 0)
184 return ret;
185
186 for (i = 0; i < ctx->pkt.nb_nals; i++) {
187 H2645NAL *nal = &ctx->pkt.nals[i];
188
189 switch (nal->type) {
190 case LCEVC_IDR_NUT:
191 s->key_frame = 1;
194 parse_nal_unit(s, avctx, nal);
195 break;
196 default:
197 break;
198 }
199 }
200
201 return 0;
202}
203
205 AVCodecContext *avctx,
206 const uint8_t **poutbuf, int *poutbuf_size,
207 const uint8_t *buf, int buf_size)
208{
209 LCEVCParserContext *ctx = s->priv_data;
210 ParseContext *pc = &ctx->pc;
211 int next;
212
213 if (!ctx->parsed_extradata && avctx->extradata_size > 4) {
214 ctx->parsed_extradata = 1;
215 ctx->is_lvcc = !!avctx->extradata[0];
216
217 if (ctx->is_lvcc)
218 ctx->nal_length_size = (avctx->extradata[4] >> 6) + 1;
219 }
220
221 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
222 next = buf_size;
223 } else {
224 next = lcevc_find_frame_end(s, buf, buf_size);
225 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
226 *poutbuf = NULL;
227 *poutbuf_size = 0;
228 return buf_size;
229 }
230 }
231
232 parse_nal_units(s, buf, buf_size, avctx);
233
234 *poutbuf = buf;
235 *poutbuf_size = buf_size;
236 return next;
237}
238
240{
241 LCEVCParserContext *ctx = s->priv_data;
242
244
245 av_freep(&ctx->pc.buffer);
246}
247
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
Definition af_astats.c:246
static AVFormatContext * ctx
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 av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2651
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC nal(CodedBitstreamContext *ctx, RWContext *rw, LCEVCRawNAL *current, int nal_unit_type)
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
bitstream reader API header.
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static void skip_bits1(GetBitContext *s)
Definition get_bits.h:416
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
@ AV_CODEC_ID_LCEVC
Definition codec_id.h:609
@ AV_PICTURE_STRUCTURE_UNKNOWN
unknown
Definition avcodec.h:2611
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition avutil.h:277
@ H2645_FLAG_SMALL_PADDING
Definition h2645_parse.h:98
@ H2645_FLAG_IS_NALFF
Definition h2645_parse.h:97
static uint64_t get_mb(GetBitContext *s)
Definition lcevc_parse.h:26
const FFCodecParser ff_lcevc_parser
static int lcevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf, int buf_size)
static void lcevc_parser_close(AVCodecParserContext *s)
static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf, int buf_size, AVCodecContext *avctx)
static int lcevc_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
static int parse_nal_unit(AVCodecParserContext *s, AVCodecContext *avctx, const H2645NAL *nal)
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, void *logctx, int nal_length_size, enum AVCodecID codec_id, int flags)
Split an input packet into NAL units.
LCEVC common definitions.
@ LCEVC_PAYLOAD_TYPE_GLOBAL_CONFIG
Definition lcevc.h:71
@ LCEVC_PAYLOAD_TYPE_SEQUENCE_CONFIG
Definition lcevc.h:70
@ LCEVC_NON_IDR_NUT
Definition lcevc.h:60
@ LCEVC_IDR_NUT
Definition lcevc.h:61
enum AVPixelFormat ff_lcevc_depth_type[4][4]
Definition lcevctab.c:38
const struct FFLCEVCDim ff_lcevc_resolution_type[63]
Definition lcevctab.c:22
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
Memory handling functions.
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 START_CODE
main external API structure.
Definition avcodec.h:443
int level
Encoding level descriptor.
Definition avcodec.h:1646
int profile
profile
Definition avcodec.h:1636
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
const uint8_t * buffer
Definition bytestream.h:34
ParseContext pc
uint32_t state
contains the last few bytes in MSB order
Definition parser.h:33
int frame_start_found
Definition parser.h:34
#define av_freep(p)