PageRenderTime 52ms CodeModel.GetById 8ms app.highlight 37ms RepoModel.GetById 2ms app.codeStats 0ms

/libavformat/bethsoftvid.c

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