History

avformat/av1dec: Deduplicate Annex B and low overhead OBU AV1 demuxer

Andreas Rheinhardt committed 5 years ago

Commit 172116f1 parent 8a805091 file · +47 −86Browse files

Unified · Split

libavformat/av1dec.c+47−86
@@ -29,6 +29,14 @@ · 28 unmodified lines
29#include "avio_internal.h"29#include "avio_internal.h"
30#include "internal.h"30#include "internal.h"
31 31
32typedef struct AV1DemuxContext {
33 const AVClass *class;
34 AVBSFContext *bsf;
35 AVRational framerate;
36 uint32_t temporal_unit_size;
37 uint32_t frame_unit_size;
38} AV1DemuxContext;
39
32//return < 0 if we need more data40//return < 0 if we need more data
33static int get_score(int type, int *seq)41static int get_score(int type, int *seq)
34{42{
@@ -48,14 +56,15 @@ static int get_score(int type, int *seq) · 13 unmodified lines
48 return 0;56 return 0;
49}57}
50 58
51static int read_header(AVFormatContext *s, const AVRational *framerate, AVBSFContext **bsf, void *logctx)59static int av1_read_header(AVFormatContext *s)
52{60{
61 AV1DemuxContext *const c = s->priv_data;
53 const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge");62 const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge");
54 AVStream *st;63 AVStream *st;
55 int ret;64 int ret;
56 65
57 if (!filter) {66 if (!filter) {
58 av_log(logctx, AV_LOG_ERROR, "av1_frame_merge bitstream filter "67 av_log(s, AV_LOG_ERROR, "av1_frame_merge bitstream filter "
59 "not found. This is a bug, please report it.\n");68 "not found. This is a bug, please report it.\n");
60 return AVERROR_BUG;69 return AVERROR_BUG;
61 }70 }
@@ -68,35 +77,49 @@ static int read_header(AVFormatContext *s, const AVRational *framerate, AVBSFCon · 6 unmodified lines
68 st->codecpar->codec_id = AV_CODEC_ID_AV1;77 st->codecpar->codec_id = AV_CODEC_ID_AV1;
69 st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;78 st->internal->need_parsing = AVSTREAM_PARSE_HEADERS;
70 79
71 st->internal->avctx->framerate = *framerate;80 st->internal->avctx->framerate = c->framerate;
72 // taken from rawvideo demuxers81 // taken from rawvideo demuxers
73 avpriv_set_pts_info(st, 64, 1, 1200000);82 avpriv_set_pts_info(st, 64, 1, 1200000);
74 83
75 ret = av_bsf_alloc(filter, bsf);84 ret = av_bsf_alloc(filter, &c->bsf);
76 if (ret < 0)85 if (ret < 0)
77 return ret;86 return ret;
78 87
79 ret = avcodec_parameters_copy((*bsf)->par_in, st->codecpar);88 ret = avcodec_parameters_copy(c->bsf->par_in, st->codecpar);
80 if (ret < 0)89 if (ret < 0)
81 return ret;90 return ret;
82 91
83 ret = av_bsf_init(*bsf);92 ret = av_bsf_init(c->bsf);
84 if (ret < 0)93 if (ret < 0)
85 return ret;94 return ret;
86 95
87 return 0;96 return 0;
88}97}
89 98
99static int av1_read_close(AVFormatContext *s)
100{
101 AV1DemuxContext *const c = s->priv_data;
102
103 av_bsf_free(&c->bsf);
104 return 0;
105}
106
90#define DEC AV_OPT_FLAG_DECODING_PARAM107#define DEC AV_OPT_FLAG_DECODING_PARAM
108#define OFFSET(x) offsetof(AV1DemuxContext, x)
109static const AVOption av1_options[] = {
110 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
111 { NULL },
112};
113#undef OFFSET
114
115static const AVClass av1_demuxer_class = {
116 .class_name = "AV1 Annex B/low overhead OBU demuxer",
117 .item_name = av_default_item_name,
118 .option = av1_options,
119 .version = LIBAVUTIL_VERSION_INT,
120};
91 121
92#if CONFIG_AV1_DEMUXER122#if CONFIG_AV1_DEMUXER
93typedef struct AnnexBContext {
94 const AVClass *class;
95 AVBSFContext *bsf;
96 uint32_t temporal_unit_size;
97 uint32_t frame_unit_size;
98 AVRational framerate;
99} AnnexBContext;
100 123
101static int leb(AVIOContext *pb, uint32_t *len) {124static int leb(AVIOContext *pb, uint32_t *len) {
102 int more, i = 0;125 int more, i = 0;
@@ -193,15 +216,9 @@ static int annexb_probe(const AVProbeData *p) · 90 unmodified lines
193 return 0;216 return 0;
194}217}
195 218
196static int annexb_read_header(AVFormatContext *s)
197{
198 AnnexBContext *c = s->priv_data;
199 return read_header(s, &c->framerate, &c->bsf, c);
200}
201
202static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt)219static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt)
203{220{
204 AnnexBContext *c = s->priv_data;221 AV1DemuxContext *const c = s->priv_data;
205 uint32_t obu_unit_size;222 uint32_t obu_unit_size;
206 int ret, len;223 int ret, len;
207 224
@@ -256,50 +273,22 @@ end: · 48 unmodified lines
256 return ret;273 return ret;
257}274}
258 275
259static int annexb_read_close(AVFormatContext *s)
260{
261 AnnexBContext *c = s->priv_data;
262
263 av_bsf_free(&c->bsf);
264 return 0;
265}
266
267#define OFFSET(x) offsetof(AnnexBContext, x)
268static const AVOption annexb_options[] = {
269 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
270 { NULL },
271};
272#undef OFFSET
273
274static const AVClass annexb_demuxer_class = {
275 .class_name = "AV1 Annex B demuxer",
276 .item_name = av_default_item_name,
277 .option = annexb_options,
278 .version = LIBAVUTIL_VERSION_INT,
279};
280
281const AVInputFormat ff_av1_demuxer = {276const AVInputFormat ff_av1_demuxer = {
282 .name = "av1",277 .name = "av1",
283 .long_name = NULL_IF_CONFIG_SMALL("AV1 Annex B"),278 .long_name = NULL_IF_CONFIG_SMALL("AV1 Annex B"),
284 .priv_data_size = sizeof(AnnexBContext),279 .priv_data_size = sizeof(AV1DemuxContext),
285 .flags_internal = FF_FMT_INIT_CLEANUP,280 .flags_internal = FF_FMT_INIT_CLEANUP,
286 .read_probe = annexb_probe,281 .read_probe = annexb_probe,
287 .read_header = annexb_read_header,282 .read_header = av1_read_header,
288 .read_packet = annexb_read_packet,283 .read_packet = annexb_read_packet,
289 .read_close = annexb_read_close,284 .read_close = av1_read_close,
290 .extensions = "obu",285 .extensions = "obu",
291 .flags = AVFMT_GENERIC_INDEX,286 .flags = AVFMT_GENERIC_INDEX,
292 .priv_class = &annexb_demuxer_class,287 .priv_class = &av1_demuxer_class,
293};288};
294#endif289#endif
295 290
296#if CONFIG_OBU_DEMUXER291#if CONFIG_OBU_DEMUXER
297typedef struct ObuContext {
298 const AVClass *class;
299 AVBSFContext *bsf;
300 AVRational framerate;
301} ObuContext;
302
303//For low overhead obu, we can't foresee the obu size before we parsed the header.292//For low overhead obu, we can't foresee the obu size before we parsed the header.
304//So, we can't use parse_obu_header here, since it will check size <= buf_size293//So, we can't use parse_obu_header here, since it will check size <= buf_size
305//see c27c7b49dc for more details294//see c27c7b49dc for more details
@@ -367,15 +356,9 @@ static int obu_probe(const AVProbeData *p) · 61 unmodified lines
367 return 0;356 return 0;
368}357}
369 358
370static int obu_read_header(AVFormatContext *s)
371{
372 ObuContext *c = s->priv_data;
373 return read_header(s, &c->framerate, &c->bsf, c);
374}
375
376static int obu_get_packet(AVFormatContext *s, AVPacket *pkt)359static int obu_get_packet(AVFormatContext *s, AVPacket *pkt)
377{360{
378 ObuContext *c = s->priv_data;361 AV1DemuxContext *const c = s->priv_data;
379 uint8_t header[MAX_OBU_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];362 uint8_t header[MAX_OBU_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
380 int64_t obu_size;363 int64_t obu_size;
381 int size;364 int size;
@@ -404,7 +387,7 @@ static int obu_get_packet(AVFormatContext *s, AVPacket *pkt) · 22 unmodified lines
404 387
405static int obu_read_packet(AVFormatContext *s, AVPacket *pkt)388static int obu_read_packet(AVFormatContext *s, AVPacket *pkt)
406{389{
407 ObuContext *c = s->priv_data;390 AV1DemuxContext *const c = s->priv_data;
408 int ret;391 int ret;
409 392
410 while (1) {393 while (1) {
@@ -431,39 +414,17 @@ static int obu_read_packet(AVFormatContext *s, AVPacket *pkt) · 20 unmodified lines
431 return ret;414 return ret;
432}415}
433 416
434static int obu_read_close(AVFormatContext *s)
435{
436 ObuContext *c = s->priv_data;
437
438 av_bsf_free(&c->bsf);
439 return 0;
440}
441
442#define OFFSET(x) offsetof(ObuContext, x)
443static const AVOption obu_options[] = {
444 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
445 { NULL },
446};
447#undef OFFSET
448
449static const AVClass obu_demuxer_class = {
450 .class_name = "AV1 low overhead OBU demuxer",
451 .item_name = av_default_item_name,
452 .option = obu_options,
453 .version = LIBAVUTIL_VERSION_INT,
454};
455
456const AVInputFormat ff_obu_demuxer = {417const AVInputFormat ff_obu_demuxer = {
457 .name = "obu",418 .name = "obu",
458 .long_name = NULL_IF_CONFIG_SMALL("AV1 low overhead OBU"),419 .long_name = NULL_IF_CONFIG_SMALL("AV1 low overhead OBU"),
459 .priv_data_size = sizeof(ObuContext),420 .priv_data_size = sizeof(AV1DemuxContext),
460 .flags_internal = FF_FMT_INIT_CLEANUP,421 .flags_internal = FF_FMT_INIT_CLEANUP,
461 .read_probe = obu_probe,422 .read_probe = obu_probe,
462 .read_header = obu_read_header,423 .read_header = av1_read_header,
463 .read_packet = obu_read_packet,424 .read_packet = obu_read_packet,
464 .read_close = obu_read_close,425 .read_close = av1_read_close,
465 .extensions = "obu",426 .extensions = "obu",
466 .flags = AVFMT_GENERIC_INDEX,427 .flags = AVFMT_GENERIC_INDEX,
467 .priv_class = &obu_demuxer_class,428 .priv_class = &av1_demuxer_class,
468};429};
469#endif430#endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t