]> git.sesse.net Git - ffmpeg/blob - libavformat/bethsoftvid.c
rtsp: Avoid a cast when calling strtol
[ffmpeg] / libavformat / bethsoftvid.c
1 /*
2  * Bethsoft VID format Demuxer
3  * Copyright (c) 2007 Nicholas Tung
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * @brief Bethesda Softworks VID (.vid) file demuxer
25  * @author Nicholas Tung [ntung (at. ntung com] (2007-03)
26  * @see http://wiki.multimedia.cx/index.php?title=Bethsoft_VID
27  * @see http://www.svatopluk.com/andux/docs/dfvid.html
28  */
29
30 #include "libavutil/intreadwrite.h"
31 #include "avformat.h"
32 #include "internal.h"
33 #include "libavcodec/bethsoftvideo.h"
34
35 #define BVID_PALETTE_SIZE 3 * 256
36
37 #define DEFAULT_SAMPLE_RATE 11111
38
39 typedef struct BVID_DemuxContext
40 {
41     int nframes;
42     int sample_rate;        /**< audio sample rate */
43     int width;              /**< video width       */
44     int height;             /**< video height      */
45     /** delay value between frames, added to individual frame delay.
46      * custom units, which will be added to other custom units (~=16ms according
47      * to free, unofficial documentation) */
48     int bethsoft_global_delay;
49     int video_index;        /**< video stream index */
50     int audio_index;        /**< audio stream index */
51     uint8_t *palette;
52
53     int is_finished;
54
55 } BVID_DemuxContext;
56
57 static int vid_probe(AVProbeData *p)
58 {
59     // little endian VID tag, file starts with "VID\0"
60     if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
61         return 0;
62
63     return AVPROBE_SCORE_MAX;
64 }
65
66 static int vid_read_header(AVFormatContext *s)
67 {
68     BVID_DemuxContext *vid = s->priv_data;
69     AVIOContext *pb = s->pb;
70
71     /* load main header. Contents:
72     *    bytes: 'V' 'I' 'D'
73     *    int16s: always_512, nframes, width, height, delay, always_14
74     */
75     avio_skip(pb, 5);
76     vid->nframes = avio_rl16(pb);
77     vid->width   = avio_rl16(pb);
78     vid->height  = avio_rl16(pb);
79     vid->bethsoft_global_delay = avio_rl16(pb);
80     avio_rl16(pb);
81
82     // wait until the first packet to create each stream
83     vid->video_index = -1;
84     vid->audio_index = -1;
85     vid->sample_rate = DEFAULT_SAMPLE_RATE;
86     s->ctx_flags |= AVFMTCTX_NOHEADER;
87
88     return 0;
89 }
90
91 #define BUFFER_PADDING_SIZE 1000
92 static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt,
93                       uint8_t block_type, AVFormatContext *s)
94 {
95     uint8_t * vidbuf_start = NULL;
96     int vidbuf_nbytes = 0;
97     int code;
98     int bytes_copied = 0;
99     int position, duration, npixels;
100     unsigned int vidbuf_capacity;
101     int ret = 0;
102     AVStream *st;
103
104     if (vid->video_index < 0) {
105         st = avformat_new_stream(s, NULL);
106         if (!st)
107             return AVERROR(ENOMEM);
108         vid->video_index = st->index;
109         if (vid->audio_index < 0) {
110             av_log_ask_for_sample(s, "No audio packet before first video "
111                                   "packet. Using default video time base.\n");
112         }
113         avpriv_set_pts_info(st, 64, 185, vid->sample_rate);
114         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
115         st->codec->codec_id   = AV_CODEC_ID_BETHSOFTVID;
116         st->codec->width      = vid->width;
117         st->codec->height     = vid->height;
118     }
119     st      = s->streams[vid->video_index];
120     npixels = st->codec->width * st->codec->height;
121
122     vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
123     if(!vidbuf_start)
124         return AVERROR(ENOMEM);
125
126     // save the file position for the packet, include block type
127     position = avio_tell(pb) - 1;
128
129     vidbuf_start[vidbuf_nbytes++] = block_type;
130
131     // get the current packet duration
132     duration = vid->bethsoft_global_delay + avio_rl16(pb);
133
134     // set the y offset if it exists (decoder header data should be in data section)
135     if(block_type == VIDEO_YOFF_P_FRAME){
136         if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2) {
137             ret = AVERROR(EIO);
138             goto fail;
139         }
140         vidbuf_nbytes += 2;
141     }
142
143     do{
144         vidbuf_start = av_fast_realloc(vidbuf_start, &vidbuf_capacity, vidbuf_nbytes + BUFFER_PADDING_SIZE);
145         if(!vidbuf_start)
146             return AVERROR(ENOMEM);
147
148         code = avio_r8(pb);
149         vidbuf_start[vidbuf_nbytes++] = code;
150
151         if(code >= 0x80){ // rle sequence
152             if(block_type == VIDEO_I_FRAME)
153                 vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
154         } else if(code){ // plain sequence
155             if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code) {
156                 ret = AVERROR(EIO);
157                 goto fail;
158             }
159             vidbuf_nbytes += code;
160         }
161         bytes_copied += code & 0x7F;
162         if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
163             // may contain a 0 byte even if read all pixels
164             if(avio_r8(pb))
165                 avio_seek(pb, -1, SEEK_CUR);
166             break;
167         }
168         if (bytes_copied > npixels) {
169             ret = AVERROR_INVALIDDATA;
170             goto fail;
171         }
172     } while(code);
173
174     // copy data into packet
175     if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
176         goto fail;
177     memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
178     av_free(vidbuf_start);
179
180     pkt->pos = position;
181     pkt->stream_index = vid->video_index;
182     pkt->duration = duration;
183     if (block_type == VIDEO_I_FRAME)
184         pkt->flags |= AV_PKT_FLAG_KEY;
185
186     /* if there is a new palette available, add it to packet side data */
187     if (vid->palette) {
188         uint8_t *pdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
189                                                  BVID_PALETTE_SIZE);
190         memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
191         av_freep(&vid->palette);
192     }
193
194     vid->nframes--;  // used to check if all the frames were read
195     return 0;
196 fail:
197     av_free(vidbuf_start);
198     return ret;
199 }
200
201 static int vid_read_packet(AVFormatContext *s,
202                            AVPacket *pkt)
203 {
204     BVID_DemuxContext *vid = s->priv_data;
205     AVIOContext *pb = s->pb;
206     unsigned char block_type;
207     int audio_length;
208     int ret_value;
209
210     if(vid->is_finished || pb->eof_reached)
211         return AVERROR(EIO);
212
213     block_type = avio_r8(pb);
214     switch(block_type){
215         case PALETTE_BLOCK:
216             if (vid->palette) {
217                 av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
218                 av_freep(&vid->palette);
219             }
220             vid->palette = av_malloc(BVID_PALETTE_SIZE);
221             if (!vid->palette)
222                 return AVERROR(ENOMEM);
223             if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) != BVID_PALETTE_SIZE) {
224                 av_freep(&vid->palette);
225                 return AVERROR(EIO);
226             }
227             return vid_read_packet(s, pkt);
228
229         case FIRST_AUDIO_BLOCK:
230             avio_rl16(pb);
231             // soundblaster DAC used for sample rate, as on specification page (link above)
232             vid->sample_rate = 1000000 / (256 - avio_r8(pb));
233         case AUDIO_BLOCK:
234             if (vid->audio_index < 0) {
235                 AVStream *st = avformat_new_stream(s, NULL);
236                 if (!st)
237                     return AVERROR(ENOMEM);
238                 vid->audio_index                 = st->index;
239                 st->codec->codec_type            = AVMEDIA_TYPE_AUDIO;
240                 st->codec->codec_id              = AV_CODEC_ID_PCM_U8;
241                 st->codec->channels              = 1;
242                 st->codec->bits_per_coded_sample = 8;
243                 st->codec->sample_rate           = vid->sample_rate;
244                 st->codec->bit_rate              = 8 * st->codec->sample_rate;
245                 st->start_time                   = 0;
246                 avpriv_set_pts_info(st, 64, 1, vid->sample_rate);
247             }
248             audio_length = avio_rl16(pb);
249             if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
250                 if (ret_value < 0)
251                     return ret_value;
252                 av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
253                 return AVERROR(EIO);
254             }
255             pkt->stream_index = vid->audio_index;
256             pkt->duration     = audio_length;
257             pkt->flags |= AV_PKT_FLAG_KEY;
258             return 0;
259
260         case VIDEO_P_FRAME:
261         case VIDEO_YOFF_P_FRAME:
262         case VIDEO_I_FRAME:
263             return read_frame(vid, pb, pkt, block_type, s);
264
265         case EOF_BLOCK:
266             if(vid->nframes != 0)
267                 av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
268             vid->is_finished = 1;
269             return AVERROR(EIO);
270         default:
271             av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
272                    block_type, block_type, block_type);
273             return AVERROR_INVALIDDATA;
274     }
275 }
276
277 static int vid_read_close(AVFormatContext *s)
278 {
279     BVID_DemuxContext *vid = s->priv_data;
280     av_freep(&vid->palette);
281     return 0;
282 }
283
284 AVInputFormat ff_bethsoftvid_demuxer = {
285     .name           = "bethsoftvid",
286     .long_name      = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
287     .priv_data_size = sizeof(BVID_DemuxContext),
288     .read_probe     = vid_probe,
289     .read_header    = vid_read_header,
290     .read_packet    = vid_read_packet,
291     .read_close     = vid_read_close,
292 };