FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/nuv.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 145 227 63.9%
Functions: 4 6 66.7%
Branches: 49 114 43.0%

Line Branch Exec Source
1 /*
2 * NuppelVideo demuxer.
3 * Copyright (c) 2006 Reimar Doeffinger
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/intfloat.h"
26 #include "avformat.h"
27 #include "avio_internal.h"
28 #include "demux.h"
29 #include "internal.h"
30 #include "riff.h"
31
32 static const AVCodecTag nuv_audio_tags[] = {
33 { AV_CODEC_ID_PCM_S16LE, MKTAG('R', 'A', 'W', 'A') },
34 { AV_CODEC_ID_MP3, MKTAG('L', 'A', 'M', 'E') },
35 { AV_CODEC_ID_NONE, 0 },
36 };
37
38 typedef struct NUVContext {
39 int v_id;
40 int a_id;
41 int rtjpg_video;
42 } NUVContext;
43
44 typedef enum {
45 NUV_VIDEO = 'V',
46 NUV_EXTRADATA = 'D',
47 NUV_AUDIO = 'A',
48 NUV_SEEKP = 'R',
49 NUV_MYTHEXT = 'X'
50 } nuv_frametype;
51
52 6782 static int nuv_probe(const AVProbeData *p)
53 {
54
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6781 times.
6782 if (!memcmp(p->buf, "NuppelVideo", 12))
55 1 return AVPROBE_SCORE_MAX;
56
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6780 times.
6781 if (!memcmp(p->buf, "MythTVVideo", 12))
57 1 return AVPROBE_SCORE_MAX;
58 6780 return 0;
59 }
60
61 /// little macro to sanitize packet size
62 #define PKTSIZE(s) (s & 0xffffff)
63
64 /**
65 * @brief read until we found all data needed for decoding
66 * @param vst video stream of which to change parameters
67 * @param ast video stream of which to change parameters
68 * @param myth set if this is a MythTVVideo format file
69 * @return 0 or AVERROR code
70 */
71 2 static int get_codec_data(AVFormatContext *s, AVIOContext *pb, AVStream *vst,
72 AVStream *ast, int myth)
73 {
74 nuv_frametype frametype;
75
76
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (!vst && !myth)
77 return 1; // no codec data needed
78
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 while (!avio_feof(pb)) {
79 int size, subtype, ret;
80
81 3 frametype = avio_r8(pb);
82
2/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 switch (frametype) {
83 2 case NUV_EXTRADATA:
84 2 subtype = avio_r8(pb);
85 2 avio_skip(pb, 6);
86 2 size = PKTSIZE(avio_rl32(pb));
87
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (vst && subtype == 'R') {
88
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = ff_get_extradata(NULL, vst->codecpar, pb, size)) < 0)
89 return ret;
90 2 size = 0;
91
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!myth)
92 1 return 0;
93 }
94 1 break;
95 1 case NUV_MYTHEXT:
96 1 avio_skip(pb, 7);
97 1 size = PKTSIZE(avio_rl32(pb));
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (size != 128 * 4)
99 break;
100 1 avio_rl32(pb); // version
101
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (vst) {
102 1 vst->codecpar->codec_tag = avio_rl32(pb);
103 2 vst->codecpar->codec_id =
104 1 ff_codec_get_id(ff_codec_bmp_tags, vst->codecpar->codec_tag);
105
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (vst->codecpar->codec_tag == MKTAG('R', 'J', 'P', 'G'))
106 1 vst->codecpar->codec_id = AV_CODEC_ID_NUV;
107 } else
108 avio_skip(pb, 4);
109
110
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ast) {
111 int id;
112
113 1 ast->codecpar->codec_tag = avio_rl32(pb);
114 1 ast->codecpar->sample_rate = avio_rl32(pb);
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ast->codecpar->sample_rate <= 0) {
116 av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", ast->codecpar->sample_rate);
117 return AVERROR_INVALIDDATA;
118 }
119 1 ast->codecpar->bits_per_coded_sample = avio_rl32(pb);
120 1 av_channel_layout_uninit(&ast->codecpar->ch_layout);
121 1 ast->codecpar->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
122 1 ast->codecpar->ch_layout.nb_channels = avio_rl32(pb);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ast->codecpar->ch_layout.nb_channels <= 0) {
124 av_log(s, AV_LOG_ERROR, "Invalid channels %d\n", ast->codecpar->ch_layout.nb_channels);
125 return AVERROR_INVALIDDATA;
126 }
127
128 2 id = ff_wav_codec_get_id(ast->codecpar->codec_tag,
129 1 ast->codecpar->bits_per_coded_sample);
130
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (id == AV_CODEC_ID_NONE) {
131 1 id = ff_codec_get_id(nuv_audio_tags, ast->codecpar->codec_tag);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (id == AV_CODEC_ID_PCM_S16LE)
133 id = ff_get_pcm_codec_id(ast->codecpar->bits_per_coded_sample,
134 0, 0, ~1);
135 }
136 1 ast->codecpar->codec_id = id;
137
138 1 ffstream(ast)->need_parsing = AVSTREAM_PARSE_FULL;
139 } else
140 avio_skip(pb, 4 * 4);
141
142 1 size -= 6 * 4;
143 1 avio_skip(pb, size);
144 1 return 0;
145 case NUV_SEEKP:
146 size = 11;
147 break;
148 default:
149 avio_skip(pb, 7);
150 size = PKTSIZE(avio_rl32(pb));
151 break;
152 }
153 1 avio_skip(pb, size);
154 }
155
156 return 0;
157 }
158
159 2 static int nuv_header(AVFormatContext *s)
160 {
161 2 NUVContext *ctx = s->priv_data;
162 2 AVIOContext *pb = s->pb;
163 char id_string[12];
164 double aspect, fps;
165 int is_mythtv, width, height, v_packs, a_packs, ret;
166 2 AVStream *vst = NULL, *ast = NULL;
167
168 2 avio_read(pb, id_string, 12);
169 2 is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
170 2 avio_skip(pb, 5); // version string
171 2 avio_skip(pb, 3); // padding
172 2 width = avio_rl32(pb);
173 2 height = avio_rl32(pb);
174 2 avio_rl32(pb); // unused, "desiredwidth"
175 2 avio_rl32(pb); // unused, "desiredheight"
176 2 avio_r8(pb); // 'P' == progressive, 'I' == interlaced
177 2 avio_skip(pb, 3); // padding
178 2 aspect = av_int2double(avio_rl64(pb));
179
3/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
2 if (aspect > 0.9999 && aspect < 1.0001)
180 1 aspect = 4.0 / 3.0;
181 2 fps = av_int2double(avio_rl64(pb));
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (fps < 0.0f) {
183 if (s->error_recognition & AV_EF_EXPLODE) {
184 av_log(s, AV_LOG_ERROR, "Invalid frame rate %f\n", fps);
185 return AVERROR_INVALIDDATA;
186 } else {
187 av_log(s, AV_LOG_WARNING, "Invalid frame rate %f, setting to 0.\n", fps);
188 fps = 0.0f;
189 }
190 }
191
192 // number of packets per stream type, -1 means unknown, e.g. streaming
193 2 v_packs = avio_rl32(pb);
194 2 a_packs = avio_rl32(pb);
195 2 avio_rl32(pb); // text
196
197 2 avio_rl32(pb); // keyframe distance (?)
198
199
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (v_packs) {
200 2 vst = avformat_new_stream(s, NULL);
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!vst)
202 return AVERROR(ENOMEM);
203 2 ctx->v_id = vst->index;
204
205 2 ret = av_image_check_size(width, height, 0, s);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
207 return ret;
208
209 2 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
210 2 vst->codecpar->codec_id = AV_CODEC_ID_NUV;
211 2 vst->codecpar->width = width;
212 2 vst->codecpar->height = height;
213 2 vst->codecpar->bits_per_coded_sample = 10;
214 2 vst->sample_aspect_ratio = av_d2q(aspect * height / width,
215 10000);
216 #if FF_API_R_FRAME_RATE
217 2 vst->r_frame_rate =
218 #endif
219 2 vst->avg_frame_rate = av_d2q(fps, 60000);
220 2 avpriv_set_pts_info(vst, 32, 1, 1000);
221 } else
222 ctx->v_id = -1;
223
224
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (a_packs) {
225 2 ast = avformat_new_stream(s, NULL);
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!ast)
227 return AVERROR(ENOMEM);
228 2 ctx->a_id = ast->index;
229
230 2 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
231 2 ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
232 2 ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
233 2 ast->codecpar->sample_rate = 44100;
234 2 ast->codecpar->bit_rate = 2 * 2 * 44100 * 8;
235 2 ast->codecpar->block_align = 2 * 2;
236 2 ast->codecpar->bits_per_coded_sample = 16;
237 2 avpriv_set_pts_info(ast, 32, 1, 1000);
238 } else
239 ctx->a_id = -1;
240
241
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = get_codec_data(s, pb, vst, ast, is_mythtv)) < 0)
242 return ret;
243
244
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 ctx->rtjpg_video = vst && vst->codecpar->codec_id == AV_CODEC_ID_NUV;
245
246 2 return 0;
247 }
248
249 #define HDRSIZE 12
250
251 162 static int nuv_packet(AVFormatContext *s, AVPacket *pkt)
252 {
253 162 NUVContext *ctx = s->priv_data;
254 162 AVIOContext *pb = s->pb;
255 uint8_t hdr[HDRSIZE];
256 nuv_frametype frametype;
257 int ret, size;
258
259
2/2
✓ Branch 1 taken 171 times.
✓ Branch 2 taken 1 times.
172 while (!avio_feof(pb)) {
260
1/2
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
171 int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
261 171 uint64_t pos = avio_tell(pb);
262
263 171 ret = ffio_read_size(pb, hdr, HDRSIZE);
264
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 170 times.
171 if (ret < 0)
265 1 return ret;
266
267 170 frametype = hdr[0];
268 170 size = PKTSIZE(AV_RL32(&hdr[8]));
269
270
4/5
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
✓ Branch 2 taken 101 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 7 times.
170 switch (frametype) {
271 case NUV_EXTRADATA:
272 if (!ctx->rtjpg_video) {
273 avio_skip(pb, size);
274 break;
275 }
276 case NUV_VIDEO:
277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (ctx->v_id < 0) {
278 av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
279 avio_skip(pb, size);
280 break;
281 }
282 59 ret = av_new_packet(pkt, copyhdrsize + size);
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (ret < 0)
284 return ret;
285
286 59 pkt->pos = pos;
287 59 pkt->flags |= hdr[2] == 0 ? AV_PKT_FLAG_KEY : 0;
288 59 pkt->pts = AV_RL32(&hdr[4]);
289 59 pkt->stream_index = ctx->v_id;
290 59 memcpy(pkt->data, hdr, copyhdrsize);
291 59 ret = avio_read(pb, pkt->data + copyhdrsize, size);
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (ret < 0) {
293 return ret;
294 }
295
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 58 times.
59 if (ret < size)
296 1 av_shrink_packet(pkt, copyhdrsize + ret);
297 59 return 0;
298 101 case NUV_AUDIO:
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (ctx->a_id < 0) {
300 av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
301 avio_skip(pb, size);
302 break;
303 }
304 101 ret = av_get_packet(pb, pkt, size);
305 101 pkt->flags |= AV_PKT_FLAG_KEY;
306 101 pkt->pos = pos;
307 101 pkt->pts = AV_RL32(&hdr[4]);
308 101 pkt->stream_index = ctx->a_id;
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (ret < 0)
310 return ret;
311 101 return 0;
312 3 case NUV_SEEKP:
313 // contains no data, size value is invalid
314 3 break;
315 7 default:
316 7 avio_skip(pb, size);
317 7 break;
318 }
319 }
320
321 1 return AVERROR(EIO);
322 }
323
324 /**
325 * \brief looks for the string RTjjjjjjjjjj in the stream too resync reading
326 * \return 1 if the syncword is found 0 otherwise.
327 */
328 static int nuv_resync(AVFormatContext *s, int64_t pos_limit) {
329 AVIOContext *pb = s->pb;
330 uint32_t tag = 0;
331 while(!avio_feof(pb) && avio_tell(pb) < pos_limit) {
332 tag = (tag << 8) | avio_r8(pb);
333 if (tag == MKBETAG('R','T','j','j') &&
334 (tag = avio_rb32(pb)) == MKBETAG('j','j','j','j') &&
335 (tag = avio_rb32(pb)) == MKBETAG('j','j','j','j'))
336 return 1;
337 }
338 return 0;
339 }
340
341 /**
342 * \brief attempts to read a timestamp from stream at the given stream position
343 * \return timestamp if successful and AV_NOPTS_VALUE if failure
344 */
345 static int64_t nuv_read_dts(AVFormatContext *s, int stream_index,
346 int64_t *ppos, int64_t pos_limit)
347 {
348 NUVContext *ctx = s->priv_data;
349 AVIOContext *pb = s->pb;
350 uint8_t hdr[HDRSIZE];
351 nuv_frametype frametype;
352 int size, key, idx;
353 int64_t pos, dts;
354
355 if (avio_seek(pb, *ppos, SEEK_SET) < 0)
356 return AV_NOPTS_VALUE;
357
358 if (!nuv_resync(s, pos_limit))
359 return AV_NOPTS_VALUE;
360
361 while (!avio_feof(pb) && avio_tell(pb) < pos_limit) {
362 if (avio_read(pb, hdr, HDRSIZE) < HDRSIZE)
363 return AV_NOPTS_VALUE;
364 frametype = hdr[0];
365 size = PKTSIZE(AV_RL32(&hdr[8]));
366 switch (frametype) {
367 case NUV_SEEKP:
368 break;
369 case NUV_AUDIO:
370 case NUV_VIDEO:
371 if (frametype == NUV_VIDEO) {
372 idx = ctx->v_id;
373 key = hdr[2] == 0;
374 } else {
375 idx = ctx->a_id;
376 key = 1;
377 }
378 if (stream_index == idx) {
379
380 pos = avio_tell(s->pb) - HDRSIZE;
381 dts = AV_RL32(&hdr[4]);
382
383 // TODO - add general support in av_gen_search, so it adds positions after reading timestamps
384 av_add_index_entry(s->streams[stream_index], pos, dts, size + HDRSIZE, 0,
385 key ? AVINDEX_KEYFRAME : 0);
386
387 *ppos = pos;
388 return dts;
389 }
390 default:
391 avio_skip(pb, size);
392 break;
393 }
394 }
395 return AV_NOPTS_VALUE;
396 }
397
398
399 const AVInputFormat ff_nuv_demuxer = {
400 .name = "nuv",
401 .long_name = NULL_IF_CONFIG_SMALL("NuppelVideo"),
402 .priv_data_size = sizeof(NUVContext),
403 .read_probe = nuv_probe,
404 .read_header = nuv_header,
405 .read_packet = nuv_packet,
406 .read_timestamp = nuv_read_dts,
407 .flags = AVFMT_GENERIC_INDEX,
408 };
409