FFmpeg
Loading...
Searching...
No Matches
evcdec.c
Go to the documentation of this file.
1/*
2 * RAW EVC video demuxer
3 *
4 * Copyright (c) 2021 Dawid Kozinski <d.kozinski@samsung.com>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include "libavcodec/evc.h"
24#include "libavcodec/bsf.h"
25
26#include "libavutil/opt.h"
27
28#include "avformat.h"
29#include "avio_internal.h"
30#include "demux.h"
31#include "evc.h"
32#include "internal.h"
33
34
35#define RAW_PACKET_SIZE 1024
36
44
45#define DEC AV_OPT_FLAG_DECODING_PARAM
46#define OFFSET(x) offsetof(EVCDemuxContext, x)
47static const AVOption evc_options[] = {
48 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
49 { NULL },
50};
51#undef OFFSET
52
53static const AVClass evc_demuxer_class = {
54 .class_name = "EVC Annex B demuxer",
55 .item_name = av_default_item_name,
56 .option = evc_options,
57 .version = LIBAVUTIL_VERSION_INT,
58};
59
60static int annexb_probe(const AVProbeData *p)
61{
62 int nalu_type;
63 size_t nalu_size;
64 int got_sps = 0, got_pps = 0, got_idr = 0, got_nonidr = 0;
65 const unsigned char *bits = p->buf;
66 int bytes_to_read = p->buf_size;
67
68 while (bytes_to_read > EVC_NALU_LENGTH_PREFIX_SIZE) {
69
71 if (nalu_size == 0) break;
72
74 bytes_to_read -= EVC_NALU_LENGTH_PREFIX_SIZE;
75
76 if(bytes_to_read < nalu_size) break;
77
78 nalu_type = evc_get_nalu_type(bits, bytes_to_read);
79
80 if (nalu_type == EVC_SPS_NUT)
81 got_sps++;
82 else if (nalu_type == EVC_PPS_NUT)
83 got_pps++;
84 else if (nalu_type == EVC_IDR_NUT )
85 got_idr++;
86 else if (nalu_type == EVC_NOIDR_NUT)
87 got_nonidr++;
88
89 bits += nalu_size;
90 bytes_to_read -= nalu_size;
91 }
92
93 if (got_sps && got_pps && (got_idr || got_nonidr > 3))
94 return AVPROBE_SCORE_EXTENSION + 1; // 1 more than .mpg
95
96 return 0;
97}
98
100{
101 AVStream *st;
102 FFStream *sti;
103 const AVBitStreamFilter *filter = av_bsf_get_by_name("evc_frame_merge");
104 EVCDemuxContext *c = s->priv_data;
105 int ret = 0;
106
107 if (!filter)
108 return AVERROR_BUG;
109
111 if (!st) {
112 ret = AVERROR(ENOMEM);
113 goto fail;
114 }
115 sti = ffstream(st);
116
119
120 // This causes sending to the parser full frames, not chunks of data
121 // The flag PARSER_FLAG_COMPLETE_FRAMES will be set in demux.c (demux.c: 1316)
123
124 st->avg_frame_rate = c->framerate;
125
126 // taken from rawvideo demuxers
127 avpriv_set_pts_info(st, 64, 1, 1200000);
128
129 ret = av_bsf_alloc(filter, &c->bsf);
130 if (ret < 0)
131 return ret;
132
133 ret = avcodec_parameters_copy(c->bsf->par_in, st->codecpar);
134 if (ret < 0)
135 return ret;
136
137 ret = av_bsf_init(c->bsf);
138 if (ret < 0)
139 return ret;
140
141fail:
142 return ret;
143}
144
146{
147 int ret;
148 uint32_t nalu_size;
149 int au_end_found = 0;
150 EVCDemuxContext *const c = s->priv_data;
151
152 while(!au_end_found) {
153 uint8_t buf[EVC_NALU_LENGTH_PREFIX_SIZE];
154
155 if (avio_feof(s->pb))
156 goto end;
157
159 if (ret < 0)
160 return ret;
161
162 ret = avio_read(s->pb, buf, EVC_NALU_LENGTH_PREFIX_SIZE);
163 if (ret < 0)
164 return ret;
166 return AVERROR_INVALIDDATA;
167
169 if (!nalu_size || nalu_size > INT_MAX)
170 return AVERROR_INVALIDDATA;
171
172 avio_seek(s->pb, -EVC_NALU_LENGTH_PREFIX_SIZE, SEEK_CUR);
173
174 ret = av_get_packet(s->pb, pkt, nalu_size + EVC_NALU_LENGTH_PREFIX_SIZE);
175 if (ret < 0)
176 return ret;
177 if (ret != (nalu_size + EVC_NALU_LENGTH_PREFIX_SIZE))
178 return AVERROR_INVALIDDATA;
179
180end:
181 ret = av_bsf_send_packet(c->bsf, pkt);
182 if (ret < 0) {
183 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
184 "evc_frame_merge filter\n");
185 return ret;
186 }
187
188 ret = av_bsf_receive_packet(c->bsf, pkt);
189 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
190 av_log(s, AV_LOG_ERROR, "evc_frame_merge filter failed to "
191 "send output packet\n");
192
193 if (ret != AVERROR(EAGAIN))
194 au_end_found = 1;
195 }
196
197 return ret;
198}
199
201{
202 EVCDemuxContext *const c = s->priv_data;
203
204 av_bsf_free(&c->bsf);
205 return 0;
206}
207
209 .p.name = "evc",
210 .p.long_name = NULL_IF_CONFIG_SMALL("EVC Annex B"),
211 .p.extensions = "evc",
213 .p.priv_class = &evc_demuxer_class,
214 .read_probe = annexb_probe,
215 .read_header = evc_read_header, // annexb_read_header
216 .read_packet = evc_read_packet, // annexb_read_packet
217 .read_close = evc_read_close,
218 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
219 .raw_codec_id = AV_CODEC_ID_EVC,
220 .priv_data_size = sizeof(EVCDemuxContext),
221};
const FFInputFormat ff_evc_demuxer
Definition evcdec.c:208
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:834
Main libavformat public API header.
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
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:98
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:498
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition avformat.h:612
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition aviobuf.c:1026
#define s(width, name)
Definition cbs_vp9.c:198
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Definition codec_par.c:107
#define NULL
Definition coverity.c:32
#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
static AVPacket * pkt
static uint32_t evc_read_nal_unit_length(const uint8_t *bits, int bits_size, void *logctx)
Definition evc_parse.h:83
static int evc_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition evcdec.c:145
static const AVOption evc_options[]
Definition evcdec.c:47
static const AVClass evc_demuxer_class
Definition evcdec.c:53
static int evc_read_close(AVFormatContext *s)
Definition evcdec.c:200
static int evc_read_header(AVFormatContext *s)
Definition evcdec.c:99
#define OFFSET(x)
Definition evcdec.c:46
static int annexb_probe(const AVProbeData *p)
Definition evcdec.c:60
static const uint8_t bits[8]
Definition fastaudio.c:100
#define fail
Definition test.h:479
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition opt.h:314
void av_bsf_free(AVBSFContext **pctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition bsf.c:47
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition bsf.c:147
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
Allocate a context for a given bitstream filter.
Definition bsf.c:99
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition bsf.c:228
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition bsf.c:200
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
@ AV_CODEC_ID_EVC
Definition codec_id.h:316
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
@ EVC_SPS_NUT
Definition evc.h:58
@ EVC_PPS_NUT
Definition evc.h:59
@ EVC_IDR_NUT
Definition evc.h:35
@ EVC_NOIDR_NUT
Definition evc.h:34
#define EVC_NALU_LENGTH_PREFIX_SIZE
Definition evc.h:26
static int evc_get_nalu_type(const uint8_t *p, int bits_size)
Definition evc.h:32
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define DEC
Definition librsvgdec.c:149
AVOptions.
The bitstream filter state.
Definition bsf.h:68
Describe the class of an AVClass context structure.
Definition log.h:76
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Rational number (pair of numerator and denominator).
Definition rational.h:58
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational avg_frame_rate
Average framerate.
Definition avformat.h:855
AVRational framerate
Definition evcdec.c:39
AVBSFContext * bsf
Definition evcdec.c:41
enum AVStreamParseType need_parsing
Definition internal.h:321
#define av_log(a,...)
float framerate
Definition av1_levels.c:29
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
static double c[64]