]> git.sesse.net Git - ffmpeg/blob - libavformat/nuv.c
152ef671d357396b28cd404098867361d4cfdec8
[ffmpeg] / libavformat / nuv.c
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 #include "avformat.h"
22 #include "riff.h"
23
24 typedef struct {
25     int v_id;
26     int a_id;
27     int rtjpg_video;
28 } NUVContext;
29
30 typedef enum {
31     NUV_VIDEO = 'V',
32     NUV_EXTRADATA = 'D',
33     NUV_AUDIO = 'A',
34     NUV_SEEKP = 'R',
35     NUV_MYTHEXT = 'X'
36 } frametype_t;
37
38 static int nuv_probe(AVProbeData *p) {
39     if (!memcmp(p->buf, "NuppelVideo", 12))
40         return AVPROBE_SCORE_MAX;
41     if (!memcmp(p->buf, "MythTVVideo", 12))
42         return AVPROBE_SCORE_MAX;
43     return 0;
44 }
45
46 //! little macro to sanitize packet size
47 #define PKTSIZE(s) (s &  0xffffff)
48
49 /**
50  * \brief read until we found all data needed for decoding
51  * \param vst video stream of which to change parameters
52  * \param ast video stream of which to change parameters
53  * \param myth set if this is a MythTVVideo format file
54  * \return 1 if all required codec data was found
55  */
56 static int get_codec_data(ByteIOContext *pb, AVStream *vst,
57                           AVStream *ast, int myth) {
58     frametype_t frametype;
59     if (!vst && !myth)
60         return 1; // no codec data needed
61     while (!url_feof(pb)) {
62         int size, subtype;
63         frametype = get_byte(pb);
64         switch (frametype) {
65             case NUV_EXTRADATA:
66                 subtype = get_byte(pb);
67                 url_fskip(pb, 6);
68                 size = PKTSIZE(get_le32(pb));
69                 if (vst && subtype == 'R') {
70                     vst->codec->extradata_size = size;
71                     vst->codec->extradata = av_malloc(size);
72                     get_buffer(pb, vst->codec->extradata, size);
73                     size = 0;
74                     if (!myth)
75                         return 1;
76                 }
77                 break;
78             case NUV_MYTHEXT:
79                 url_fskip(pb, 7);
80                 size = PKTSIZE(get_le32(pb));
81                 if (size != 128 * 4)
82                     break;
83                 get_le32(pb); // version
84                 if (vst) {
85                     vst->codec->codec_tag = get_le32(pb);
86                     vst->codec->codec_id =
87                         codec_get_id(codec_bmp_tags, vst->codec->codec_tag);
88                 } else
89                     url_fskip(pb, 4);
90
91                 if (ast) {
92                     ast->codec->codec_tag = get_le32(pb);
93                     ast->codec->sample_rate = get_le32(pb);
94                     ast->codec->bits_per_sample = get_le32(pb);
95                     ast->codec->channels = get_le32(pb);
96                     ast->codec->codec_id =
97                         wav_codec_get_id(ast->codec->codec_tag,
98                                          ast->codec->bits_per_sample);
99                     ast->need_parsing = AVSTREAM_PARSE_FULL;
100                 } else
101                     url_fskip(pb, 4 * 4);
102
103                 size -= 6 * 4;
104                 url_fskip(pb, size);
105                 return 1;
106             case NUV_SEEKP:
107                 size = 11;
108                 break;
109             default:
110                 url_fskip(pb, 7);
111                 size = PKTSIZE(get_le32(pb));
112                 break;
113         }
114         url_fskip(pb, size);
115     }
116     return 0;
117 }
118
119 static int nuv_header(AVFormatContext *s, AVFormatParameters *ap) {
120     NUVContext *ctx = s->priv_data;
121     ByteIOContext *pb = &s->pb;
122     char id_string[12], version_string[5];
123     double aspect, fps;
124     int is_mythtv, width, height, v_packs, a_packs;
125     int stream_nr = 0;
126     AVStream *vst = NULL, *ast = NULL;
127     get_buffer(pb, id_string, 12);
128     is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
129     get_buffer(pb, version_string, 5);
130     url_fskip(pb, 3); // padding
131     width = get_le32(pb);
132     height = get_le32(pb);
133     get_le32(pb); // unused, "desiredwidth"
134     get_le32(pb); // unused, "desiredheight"
135     get_byte(pb); // 'P' == progressive, 'I' == interlaced
136     url_fskip(pb, 3); // padding
137     aspect = av_int2dbl(get_le64(pb));
138     fps = av_int2dbl(get_le64(pb));
139
140     // number of packets per stream type, -1 means unknown, e.g. streaming
141     v_packs = get_le32(pb);
142     a_packs = get_le32(pb);
143     get_le32(pb); // text
144
145     get_le32(pb); // keyframe distance (?)
146
147     if (v_packs) {
148         ctx->v_id = stream_nr++;
149         vst = av_new_stream(s, ctx->v_id);
150         vst->codec->codec_type = CODEC_TYPE_VIDEO;
151         vst->codec->codec_id = CODEC_ID_NUV;
152         vst->codec->width = width;
153         vst->codec->height = height;
154         vst->codec->bits_per_sample = 10;
155         vst->codec->sample_aspect_ratio = av_d2q(aspect, 10000);
156         vst->r_frame_rate = av_d2q(fps, 60000);
157         av_set_pts_info(vst, 32, 1, 1000);
158     } else
159         ctx->v_id = -1;
160
161     if (a_packs) {
162         ctx->a_id = stream_nr++;
163         ast = av_new_stream(s, ctx->a_id);
164         ast->codec->codec_type = CODEC_TYPE_AUDIO;
165         ast->codec->codec_id = CODEC_ID_PCM_S16LE;
166         ast->codec->channels = 2;
167         ast->codec->sample_rate = 44100;
168         ast->codec->bit_rate = 2 * 2 * 44100 * 8;
169         ast->codec->block_align = 2 * 2;
170         ast->codec->bits_per_sample = 16;
171         av_set_pts_info(ast, 32, 1, 1000);
172     } else
173         ctx->a_id = -1;
174
175     get_codec_data(pb, vst, ast, is_mythtv);
176     ctx->rtjpg_video = vst->codec->codec_id == CODEC_ID_NUV;
177     ctx->rtjpg_video |= vst->codec->codec_tag == MKTAG('R', 'J', 'P', 'G');
178     return 0;
179 }
180
181 #define HDRSIZE 12
182
183 static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
184     NUVContext *ctx = s->priv_data;
185     ByteIOContext *pb = &s->pb;
186     uint8_t hdr[HDRSIZE];
187     frametype_t frametype;
188     int ret, size;
189     while (!url_feof(pb)) {
190         int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
191         ret = get_buffer(pb, hdr, HDRSIZE);
192         if (ret <= 0)
193             return ret ? ret : -1;
194         frametype = hdr[0];
195         size = PKTSIZE(AV_RL32(&hdr[8]));
196         switch (frametype) {
197             case NUV_EXTRADATA:
198                 if (!ctx->rtjpg_video) {
199                     url_fskip(pb, size);
200                     break;
201                 }
202             case NUV_VIDEO:
203                 if (ctx->v_id < 0) {
204                     av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
205                     url_fskip(pb, size);
206                     break;
207                 }
208                 ret = av_new_packet(pkt, copyhdrsize + size);
209                 if (ret < 0)
210                     return ret;
211                 pkt->pos = url_ftell(pb) - copyhdrsize;
212                 pkt->pts = AV_RL32(&hdr[4]);
213                 pkt->stream_index = ctx->v_id;
214                 memcpy(pkt->data, hdr, copyhdrsize);
215                 ret = get_buffer(pb, pkt->data + copyhdrsize, size);
216                 return ret;
217             case NUV_AUDIO:
218                 if (ctx->a_id < 0) {
219                     av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
220                     url_fskip(pb, size);
221                     break;
222                 }
223                 ret = av_get_packet(pb, pkt, size);
224                 pkt->pts = AV_RL32(&hdr[4]);
225                 pkt->stream_index = ctx->a_id;
226                 return ret;
227             case NUV_SEEKP:
228                 // contains no data, size value is invalid
229                 break;
230             default:
231                 url_fskip(pb, size);
232                 break;
233         }
234     }
235     return AVERROR(EIO);
236 }
237
238 AVInputFormat nuv_demuxer = {
239     "nuv",
240     "NuppelVideo format",
241     sizeof(NUVContext),
242     nuv_probe,
243     nuv_header,
244     nuv_packet,
245     NULL,
246     NULL,
247 };