Replace all occurrences of AVERROR_IO with AVERROR(EIO).
[libav.git] / libavformat / nuv.c
CommitLineData
dfca23e3
RD
1/*
2 * NuppelVideo demuxer.
3 * Copyright (c) 2006 Reimar Doeffinger.
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
dfca23e3
RD
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
dfca23e3 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
dfca23e3
RD
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
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
dfca23e3
RD
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21#include "avformat.h"
9d9f4119 22#include "riff.h"
dfca23e3
RD
23
24typedef struct {
25 int v_id;
26 int a_id;
27} NUVContext;
28
29typedef enum {
30 NUV_VIDEO = 'V',
31 NUV_EXTRADATA = 'D',
32 NUV_AUDIO = 'A',
33 NUV_SEEKP = 'R',
34 NUV_MYTHEXT = 'X'
35} frametype_t;
36
37static int nuv_probe(AVProbeData *p) {
dfca23e3
RD
38 if (!memcmp(p->buf, "NuppelVideo", 12))
39 return AVPROBE_SCORE_MAX;
40 if (!memcmp(p->buf, "MythTVVideo", 12))
41 return AVPROBE_SCORE_MAX;
42 return 0;
43}
44
45//! little macro to sanitize packet size
46#define PKTSIZE(s) (s & 0xffffff)
47
48/**
49 * \brief read until we found all data needed for decoding
50 * \param vst video stream of which to change parameters
51 * \param ast video stream of which to change parameters
52 * \param myth set if this is a MythTVVideo format file
53 * \return 1 if all required codec data was found
54 */
55static int get_codec_data(ByteIOContext *pb, AVStream *vst,
56 AVStream *ast, int myth) {
57 frametype_t frametype;
58 if (!vst && !myth)
59 return 1; // no codec data needed
60 while (!url_feof(pb)) {
61 int size, subtype;
62 frametype = get_byte(pb);
63 switch (frametype) {
64 case NUV_EXTRADATA:
65 subtype = get_byte(pb);
66 url_fskip(pb, 6);
67 size = PKTSIZE(get_le32(pb));
50a6c318 68 if (vst && subtype == 'R') {
dfca23e3
RD
69 vst->codec->extradata_size = size;
70 vst->codec->extradata = av_malloc(size);
71 get_buffer(pb, vst->codec->extradata, size);
72 size = 0;
73 if (!myth)
74 return 1;
75 }
76 break;
77 case NUV_MYTHEXT:
78 url_fskip(pb, 7);
79 size = PKTSIZE(get_le32(pb));
80 if (size != 128 * 4)
81 break;
82 get_le32(pb); // version
83 if (vst) {
84 vst->codec->codec_tag = get_le32(pb);
85 vst->codec->codec_id =
86 codec_get_id(codec_bmp_tags, vst->codec->codec_tag);
87 } else
88 url_fskip(pb, 4);
89
90 if (ast) {
91 ast->codec->codec_tag = get_le32(pb);
92 ast->codec->sample_rate = get_le32(pb);
93 ast->codec->bits_per_sample = get_le32(pb);
94 ast->codec->channels = get_le32(pb);
95 ast->codec->codec_id =
96 wav_codec_get_id(ast->codec->codec_tag,
97 ast->codec->bits_per_sample);
8a59d9a3 98 ast->need_parsing = AVSTREAM_PARSE_FULL;
dfca23e3
RD
99 } else
100 url_fskip(pb, 4 * 4);
101
102 size -= 6 * 4;
103 url_fskip(pb, size);
104 return 1;
105 case NUV_SEEKP:
106 size = 11;
107 break;
108 default:
109 url_fskip(pb, 7);
110 size = PKTSIZE(get_le32(pb));
111 break;
112 }
113 url_fskip(pb, size);
114 }
115 return 0;
116}
117
118static int nuv_header(AVFormatContext *s, AVFormatParameters *ap) {
e4141433 119 NUVContext *ctx = s->priv_data;
dfca23e3
RD
120 ByteIOContext *pb = &s->pb;
121 char id_string[12], version_string[5];
122 double aspect, fps;
123 int is_mythtv, width, height, v_packs, a_packs;
124 int stream_nr = 0;
125 AVStream *vst = NULL, *ast = NULL;
126 get_buffer(pb, id_string, 12);
127 is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
128 get_buffer(pb, version_string, 5);
129 url_fskip(pb, 3); // padding
130 width = get_le32(pb);
131 height = get_le32(pb);
132 get_le32(pb); // unused, "desiredwidth"
133 get_le32(pb); // unused, "desiredheight"
134 get_byte(pb); // 'P' == progressive, 'I' == interlaced
135 url_fskip(pb, 3); // padding
136 aspect = av_int2dbl(get_le64(pb));
137 fps = av_int2dbl(get_le64(pb));
138
139 // number of packets per stream type, -1 means unknown, e.g. streaming
140 v_packs = get_le32(pb);
141 a_packs = get_le32(pb);
142 get_le32(pb); // text
143
144 get_le32(pb); // keyframe distance (?)
145
146 if (v_packs) {
147 ctx->v_id = stream_nr++;
148 vst = av_new_stream(s, ctx->v_id);
149 vst->codec->codec_type = CODEC_TYPE_VIDEO;
150 vst->codec->codec_id = CODEC_ID_NUV;
151 vst->codec->codec_tag = MKTAG('R', 'J', 'P', 'G');
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);
8643594c 156 vst->r_frame_rate = av_d2q(fps, 60000);
dfca23e3
RD
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 return 0;
177}
178
179#define HDRSIZE 12
180
181static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
e4141433 182 NUVContext *ctx = s->priv_data;
dfca23e3
RD
183 ByteIOContext *pb = &s->pb;
184 uint8_t hdr[HDRSIZE];
185 frametype_t frametype;
186 int ret, size;
187 while (!url_feof(pb)) {
188 ret = get_buffer(pb, hdr, HDRSIZE);
189 if (ret <= 0)
a7b286e8 190 return ret ? ret : -1;
dfca23e3 191 frametype = hdr[0];
fead30d4 192 size = PKTSIZE(AV_RL32(&hdr[8]));
dfca23e3
RD
193 switch (frametype) {
194 case NUV_VIDEO:
195 case NUV_EXTRADATA:
196 if (ctx->v_id < 0) {
197 av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
198 url_fskip(pb, size);
199 break;
200 }
201 ret = av_new_packet(pkt, HDRSIZE + size);
202 if (ret < 0)
203 return ret;
204 pkt->pos = url_ftell(pb);
fead30d4 205 pkt->pts = AV_RL32(&hdr[4]);
dfca23e3
RD
206 pkt->stream_index = ctx->v_id;
207 memcpy(pkt->data, hdr, HDRSIZE);
208 ret = get_buffer(pb, pkt->data + HDRSIZE, size);
209 return ret;
210 case NUV_AUDIO:
211 if (ctx->a_id < 0) {
212 av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
213 url_fskip(pb, size);
214 break;
215 }
216 ret = av_get_packet(pb, pkt, size);
fead30d4 217 pkt->pts = AV_RL32(&hdr[4]);
dfca23e3
RD
218 pkt->stream_index = ctx->a_id;
219 return ret;
220 case NUV_SEEKP:
221 // contains no data, size value is invalid
222 break;
223 default:
224 url_fskip(pb, size);
225 break;
226 }
227 }
6f3e0b21 228 return AVERROR(EIO);
dfca23e3
RD
229}
230
ff70e601 231AVInputFormat nuv_demuxer = {
dfca23e3
RD
232 "nuv",
233 "NuppelVideo format",
234 sizeof(NUVContext),
235 nuv_probe,
236 nuv_header,
237 nuv_packet,
238 NULL,
239 NULL,
240};