2 * Discworld II BMV demuxer
3 * Copyright (c) 2011 Konstantin Shishkov.
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/channel_layout.h"
35 typedef struct BMVContext {
42 static int bmv_read_header(AVFormatContext *s)
45 BMVContext *c = s->priv_data;
47 st = avformat_new_stream(s, 0);
49 return AVERROR(ENOMEM);
50 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
51 st->codec->codec_id = AV_CODEC_ID_BMV_VIDEO;
52 st->codec->width = 640;
53 st->codec->height = 429;
54 st->codec->pix_fmt = AV_PIX_FMT_PAL8;
55 avpriv_set_pts_info(st, 16, 1, 12);
56 ast = avformat_new_stream(s, 0);
58 return AVERROR(ENOMEM);
59 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
60 ast->codec->codec_id = AV_CODEC_ID_BMV_AUDIO;
61 ast->codec->channels = 2;
62 ast->codec->channel_layout = AV_CH_LAYOUT_STEREO;
63 ast->codec->sample_rate = 22050;
64 avpriv_set_pts_info(ast, 16, 1, 22050);
71 static int bmv_read_packet(AVFormatContext *s, AVPacket *pkt)
73 BMVContext *c = s->priv_data;
77 if (s->pb->eof_reached)
79 type = avio_r8(s->pb);
84 c->size = avio_rl24(s->pb);
86 return AVERROR_INVALIDDATA;
87 if ((err = av_reallocp(&c->packet, c->size + 1)) < 0)
90 if (avio_read(s->pb, c->packet + 1, c->size) != c->size)
92 if (type & BMV_AUDIO) {
93 int audio_size = c->packet[1] * 65 + 1;
94 if (audio_size >= c->size) {
95 av_log(s, AV_LOG_ERROR, "Reported audio size %d is bigger than packet size (%d)\n",
97 return AVERROR_INVALIDDATA;
99 if (av_new_packet(pkt, audio_size) < 0)
100 return AVERROR(ENOMEM);
101 memcpy(pkt->data, c->packet + 1, pkt->size);
102 pkt->stream_index = 1;
103 pkt->pts = c->audio_pos;
104 pkt->duration = c->packet[1] * 32;
105 c->audio_pos += pkt->duration;
111 if (av_new_packet(pkt, c->size + 1) < 0)
112 return AVERROR(ENOMEM);
113 pkt->stream_index = 0;
115 memcpy(pkt->data, c->packet, pkt->size);
119 static int bmv_read_close(AVFormatContext *s)
121 BMVContext *c = s->priv_data;
123 av_freep(&c->packet);
128 AVInputFormat ff_bmv_demuxer = {
130 .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV"),
131 .priv_data_size = sizeof(BMVContext),
132 .read_header = bmv_read_header,
133 .read_packet = bmv_read_packet,
134 .read_close = bmv_read_close,