FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/nuv.c
Date: 2024-11-20 23:03:26
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/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 7186 static int nuv_probe(const AVProbeData *p)
53 {
54
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7185 times.
7186 if (!memcmp(p->buf, "NuppelVideo", 12))
55 1 return AVPROBE_SCORE_MAX;
56
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7184 times.
7185 if (!memcmp(p->buf, "MythTVVideo", 12))
57 1 return AVPROBE_SCORE_MAX;
58 7184 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = ffio_read_size(pb, id_string, 12)) < 0)
169 return ret;
170
171 2 is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
172 2 avio_skip(pb, 5); // version string
173 2 avio_skip(pb, 3); // padding
174 2 width = avio_rl32(pb);
175 2 height = avio_rl32(pb);
176 2 avio_rl32(pb); // unused, "desiredwidth"
177 2 avio_rl32(pb); // unused, "desiredheight"
178 2 avio_r8(pb); // 'P' == progressive, 'I' == interlaced
179 2 avio_skip(pb, 3); // padding
180 2 aspect = av_int2double(avio_rl64(pb));
181
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)
182 1 aspect = 4.0 / 3.0;
183 2 fps = av_int2double(avio_rl64(pb));
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (fps < 0.0f) {
185 if (s->error_recognition & AV_EF_EXPLODE) {
186 av_log(s, AV_LOG_ERROR, "Invalid frame rate %f\n", fps);
187 return AVERROR_INVALIDDATA;
188 } else {
189 av_log(s, AV_LOG_WARNING, "Invalid frame rate %f, setting to 0.\n", fps);
190 fps = 0.0f;
191 }
192 }
193
194 // number of packets per stream type, -1 means unknown, e.g. streaming
195 2 v_packs = avio_rl32(pb);
196 2 a_packs = avio_rl32(pb);
197 2 avio_rl32(pb); // text
198
199 2 avio_rl32(pb); // keyframe distance (?)
200
201
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (v_packs) {
202 2 vst = avformat_new_stream(s, NULL);
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!vst)
204 return AVERROR(ENOMEM);
205 2 ctx->v_id = vst->index;
206
207 2 ret = av_image_check_size(width, height, 0, s);
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
209 return ret;
210
211 2 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
212 2 vst->codecpar->codec_id = AV_CODEC_ID_NUV;
213 2 vst->codecpar->width = width;
214 2 vst->codecpar->height = height;
215 2 vst->codecpar->bits_per_coded_sample = 10;
216 2 vst->sample_aspect_ratio = av_d2q(aspect * height / width,
217 10000);
218 #if FF_API_R_FRAME_RATE
219 2 vst->r_frame_rate =
220 #endif
221 2 vst->avg_frame_rate = av_d2q(fps, 60000);
222 2 avpriv_set_pts_info(vst, 32, 1, 1000);
223 } else
224 ctx->v_id = -1;
225
226
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (a_packs) {
227 2 ast = avformat_new_stream(s, NULL);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!ast)
229 return AVERROR(ENOMEM);
230 2 ctx->a_id = ast->index;
231
232 2 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
233 2 ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
234 2 ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
235 2 ast->codecpar->sample_rate = 44100;
236 2 ast->codecpar->bit_rate = 2 * 2 * 44100 * 8;
237 2 ast->codecpar->block_align = 2 * 2;
238 2 ast->codecpar->bits_per_coded_sample = 16;
239 2 avpriv_set_pts_info(ast, 32, 1, 1000);
240 } else
241 ctx->a_id = -1;
242
243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = get_codec_data(s, pb, vst, ast, is_mythtv)) < 0)
244 return ret;
245
246
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;
247
248 2 return 0;
249 }
250
251 #define HDRSIZE 12
252
253 162 static int nuv_packet(AVFormatContext *s, AVPacket *pkt)
254 {
255 162 NUVContext *ctx = s->priv_data;
256 162 AVIOContext *pb = s->pb;
257 uint8_t hdr[HDRSIZE];
258 nuv_frametype frametype;
259 int ret, size;
260
261
2/2
✓ Branch 1 taken 171 times.
✓ Branch 2 taken 1 times.
172 while (!avio_feof(pb)) {
262
1/2
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
171 int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
263 171 uint64_t pos = avio_tell(pb);
264
265 171 ret = ffio_read_size(pb, hdr, HDRSIZE);
266
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 170 times.
171 if (ret < 0)
267 1 return ret;
268
269 170 frametype = hdr[0];
270 170 size = PKTSIZE(AV_RL32(&hdr[8]));
271
272
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) {
273 case NUV_EXTRADATA:
274 if (!ctx->rtjpg_video) {
275 avio_skip(pb, size);
276 break;
277 }
278 case NUV_VIDEO:
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (ctx->v_id < 0) {
280 av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
281 avio_skip(pb, size);
282 break;
283 }
284 59 ret = av_new_packet(pkt, copyhdrsize + size);
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (ret < 0)
286 return ret;
287
288 59 pkt->pos = pos;
289 59 pkt->flags |= hdr[2] == 0 ? AV_PKT_FLAG_KEY : 0;
290 59 pkt->pts = AV_RL32(&hdr[4]);
291 59 pkt->stream_index = ctx->v_id;
292 59 memcpy(pkt->data, hdr, copyhdrsize);
293 59 ret = avio_read(pb, pkt->data + copyhdrsize, size);
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (ret < 0) {
295 return ret;
296 }
297
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 58 times.
59 if (ret < size)
298 1 av_shrink_packet(pkt, copyhdrsize + ret);
299 59 return 0;
300 101 case NUV_AUDIO:
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (ctx->a_id < 0) {
302 av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
303 avio_skip(pb, size);
304 break;
305 }
306 101 ret = av_get_packet(pb, pkt, size);
307 101 pkt->flags |= AV_PKT_FLAG_KEY;
308 101 pkt->pos = pos;
309 101 pkt->pts = AV_RL32(&hdr[4]);
310 101 pkt->stream_index = ctx->a_id;
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (ret < 0)
312 return ret;
313 101 return 0;
314 3 case NUV_SEEKP:
315 // contains no data, size value is invalid
316 3 break;
317 7 default:
318 7 avio_skip(pb, size);
319 7 break;
320 }
321 }
322
323 1 return AVERROR(EIO);
324 }
325
326 /**
327 * \brief looks for the string RTjjjjjjjjjj in the stream too resync reading
328 * \return 1 if the syncword is found 0 otherwise.
329 */
330 static int nuv_resync(AVFormatContext *s, int64_t pos_limit) {
331 AVIOContext *pb = s->pb;
332 uint32_t tag = 0;
333 while(!avio_feof(pb) && avio_tell(pb) < pos_limit) {
334 tag = (tag << 8) | avio_r8(pb);
335 if (tag == MKBETAG('R','T','j','j') &&
336 (tag = avio_rb32(pb)) == MKBETAG('j','j','j','j') &&
337 (tag = avio_rb32(pb)) == MKBETAG('j','j','j','j'))
338 return 1;
339 }
340 return 0;
341 }
342
343 /**
344 * \brief attempts to read a timestamp from stream at the given stream position
345 * \return timestamp if successful and AV_NOPTS_VALUE if failure
346 */
347 static int64_t nuv_read_dts(AVFormatContext *s, int stream_index,
348 int64_t *ppos, int64_t pos_limit)
349 {
350 NUVContext *ctx = s->priv_data;
351 AVIOContext *pb = s->pb;
352 uint8_t hdr[HDRSIZE];
353 nuv_frametype frametype;
354 int size, key, idx;
355 int64_t pos, dts;
356
357 if (avio_seek(pb, *ppos, SEEK_SET) < 0)
358 return AV_NOPTS_VALUE;
359
360 if (!nuv_resync(s, pos_limit))
361 return AV_NOPTS_VALUE;
362
363 while (!avio_feof(pb) && avio_tell(pb) < pos_limit) {
364 if (avio_read(pb, hdr, HDRSIZE) < HDRSIZE)
365 return AV_NOPTS_VALUE;
366 frametype = hdr[0];
367 size = PKTSIZE(AV_RL32(&hdr[8]));
368 switch (frametype) {
369 case NUV_SEEKP:
370 break;
371 case NUV_AUDIO:
372 case NUV_VIDEO:
373 if (frametype == NUV_VIDEO) {
374 idx = ctx->v_id;
375 key = hdr[2] == 0;
376 } else {
377 idx = ctx->a_id;
378 key = 1;
379 }
380 if (stream_index == idx) {
381
382 pos = avio_tell(s->pb) - HDRSIZE;
383 dts = AV_RL32(&hdr[4]);
384
385 // TODO - add general support in av_gen_search, so it adds positions after reading timestamps
386 av_add_index_entry(s->streams[stream_index], pos, dts, size + HDRSIZE, 0,
387 key ? AVINDEX_KEYFRAME : 0);
388
389 *ppos = pos;
390 return dts;
391 }
392 default:
393 avio_skip(pb, size);
394 break;
395 }
396 }
397 return AV_NOPTS_VALUE;
398 }
399
400
401 const FFInputFormat ff_nuv_demuxer = {
402 .p.name = "nuv",
403 .p.long_name = NULL_IF_CONFIG_SMALL("NuppelVideo"),
404 .p.flags = AVFMT_GENERIC_INDEX,
405 .priv_data_size = sizeof(NUVContext),
406 .read_probe = nuv_probe,
407 .read_header = nuv_header,
408 .read_packet = nuv_packet,
409 .read_timestamp = nuv_read_dts,
410 };
411