FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/nuv.c
Date: 2026-05-03 13:33:45
Exec Total Coverage
Lines: 145 228 63.6%
Functions: 4 6 66.7%
Branches: 50 116 43.1%

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