tools/sofa2wavs: fix build on Windows
[ffmpeg.git] / libavformat / evcdec.c
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
37 typedef struct EVCDemuxContext {
38 const AVClass *class;
39 AVRational framerate;
40
41 AVBSFContext *bsf;
42
43 } EVCDemuxContext;
44
45 #define DEC AV_OPT_FLAG_DECODING_PARAM
46 #define OFFSET(x) offsetof(EVCDemuxContext, x)
47 static 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
53 static 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
60 static 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
70 nalu_size = evc_read_nal_unit_length(bits, EVC_NALU_LENGTH_PREFIX_SIZE);
71 if (nalu_size == 0) break;
72
73 bits += EVC_NALU_LENGTH_PREFIX_SIZE;
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
99 static int evc_read_header(AVFormatContext *s)
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 st = avformat_new_stream(s, NULL);
108 if (!st) {
109 ret = AVERROR(ENOMEM);
110 goto fail;
111 }
112 sti = ffstream(st);
113
114 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
115 st->codecpar->codec_id = AV_CODEC_ID_EVC;
116
117 // This causes sending to the parser full frames, not chunks of data
118 // The flag PARSER_FLAG_COMPLETE_FRAMES will be set in demux.c (demux.c: 1316)
119 sti->need_parsing = AVSTREAM_PARSE_HEADERS;
120
121 st->avg_frame_rate = c->framerate;
122
123 // taken from rawvideo demuxers
124 avpriv_set_pts_info(st, 64, 1, 1200000);
125
126 ret = av_bsf_alloc(filter, &c->bsf);
127 if (ret < 0)
128 return ret;
129
130 ret = avcodec_parameters_copy(c->bsf->par_in, st->codecpar);
131 if (ret < 0)
132 return ret;
133
134 ret = av_bsf_init(c->bsf);
135 if (ret < 0)
136 return ret;
137
138 fail:
139 return ret;
140 }
141
142 static int evc_read_packet(AVFormatContext *s, AVPacket *pkt)
143 {
144 int ret;
145 uint32_t nalu_size;
146 int au_end_found = 0;
147 EVCDemuxContext *const c = s->priv_data;
148
149 while(!au_end_found) {
150 uint8_t buf[EVC_NALU_LENGTH_PREFIX_SIZE];
151
152 if (avio_feof(s->pb))
153 goto end;
154
155 ret = ffio_ensure_seekback(s->pb, EVC_NALU_LENGTH_PREFIX_SIZE);
156 if (ret < 0)
157 return ret;
158
159 ret = avio_read(s->pb, buf, EVC_NALU_LENGTH_PREFIX_SIZE);
160 if (ret < 0)
161 return ret;
162 if (ret != EVC_NALU_LENGTH_PREFIX_SIZE)
163 return AVERROR_INVALIDDATA;
164
165 nalu_size = evc_read_nal_unit_length(buf, EVC_NALU_LENGTH_PREFIX_SIZE);
166 if (!nalu_size || nalu_size > INT_MAX)
167 return AVERROR_INVALIDDATA;
168
169 avio_seek(s->pb, -EVC_NALU_LENGTH_PREFIX_SIZE, SEEK_CUR);
170
171 ret = av_get_packet(s->pb, pkt, nalu_size + EVC_NALU_LENGTH_PREFIX_SIZE);
172 if (ret < 0)
173 return ret;
174 if (ret != (nalu_size + EVC_NALU_LENGTH_PREFIX_SIZE))
175 return AVERROR_INVALIDDATA;
176
177 end:
178 ret = av_bsf_send_packet(c->bsf, pkt);
179 if (ret < 0) {
180 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
181 "evc_frame_merge filter\n");
182 return ret;
183 }
184
185 ret = av_bsf_receive_packet(c->bsf, pkt);
186 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
187 av_log(s, AV_LOG_ERROR, "evc_frame_merge filter failed to "
188 "send output packet\n");
189
190 if (ret != AVERROR(EAGAIN))
191 au_end_found = 1;
192 }
193
194 return ret;
195 }
196
197 static int evc_read_close(AVFormatContext *s)
198 {
199 EVCDemuxContext *const c = s->priv_data;
200
201 av_bsf_free(&c->bsf);
202 return 0;
203 }
204
205 const FFInputFormat ff_evc_demuxer = {
206 .p.name = "evc",
207 .p.long_name = NULL_IF_CONFIG_SMALL("EVC Annex B"),
208 .p.extensions = "evc",
209 .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NOTIMESTAMPS,
210 .p.priv_class = &evc_demuxer_class,
211 .read_probe = annexb_probe,
212 .read_header = evc_read_header, // annexb_read_header
213 .read_packet = evc_read_packet, // annexb_read_packet
214 .read_close = evc_read_close,
215 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
216 .raw_codec_id = AV_CODEC_ID_EVC,
217 .priv_data_size = sizeof(EVCDemuxContext),
218 };