1/* 2 * Discworld II BMV demuxer 3 * Copyright (c) 2011 Konstantin Shishkov 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#include "libavutil/channel_layout.h" 23#include "avformat.h" 24#include "internal.h" 25 26enum BMVFlags { 27 BMV_NOP = 0, 28 BMV_END, 29 BMV_DELTA, 30 BMV_INTRA, 31 32 BMV_AUDIO = 0x20, 33}; 34 35typedef struct BMVContext { 36 uint8_t *packet; 37 int size; 38 int get_next; 39 int64_t audio_pos; 40} BMVContext; 41 42static int bmv_read_header(AVFormatContext *s) 43{ 44 AVStream *st, *ast; 45 BMVContext *c = s->priv_data; 46 47 st = avformat_new_stream(s, 0); 48 if (!st) 49 return AVERROR(ENOMEM); 50 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; 51 st->codecpar->codec_id = AV_CODEC_ID_BMV_VIDEO; 52 st->codecpar->width = 640; 53 st->codecpar->height = 429; 54 st->codecpar->format = AV_PIX_FMT_PAL8; 55 avpriv_set_pts_info(st, 16, 1, 12); 56 ast = avformat_new_stream(s, 0); 57 if (!ast) 58 return AVERROR(ENOMEM); 59 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; 60 ast->codecpar->codec_id = AV_CODEC_ID_BMV_AUDIO; 61 ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; 62 ast->codecpar->sample_rate = 22050; 63 avpriv_set_pts_info(ast, 16, 1, 22050); 64 65 c->get_next = 1; 66 c->audio_pos = 0; 67 return 0; 68} 69 70static int bmv_read_packet(AVFormatContext *s, AVPacket *pkt) 71{ 72 BMVContext *c = s->priv_data; 73 int type, err; 74 75 while (c->get_next) { 76 if (s->pb->eof_reached) 77 return AVERROR_EOF; 78 type = avio_r8(s->pb); 79 if (type == BMV_NOP) 80 continue; 81 if (type == BMV_END) 82 return AVERROR_EOF; 83 c->size = avio_rl24(s->pb); 84 if (!c->size) 85 return AVERROR_INVALIDDATA; 86 if ((err = av_reallocp(&c->packet, c->size + 1)) < 0) 87 return err; 88 c->packet[0] = type; 89 if (avio_read(s->pb, c->packet + 1, c->size) != c->size) 90 return AVERROR(EIO); 91 if (type & BMV_AUDIO) { 92 int audio_size = c->packet[1] * 65 + 1; 93 if (audio_size >= c->size) { 94 av_log(s, AV_LOG_ERROR, "Reported audio size %d is bigger than packet size (%d)\n", 95 audio_size, c->size); 96 return AVERROR_INVALIDDATA; 97 } 98 if ((err = av_new_packet(pkt, audio_size)) < 0) 99 return err; 100 memcpy(pkt->data, c->packet + 1, pkt->size); 101 pkt->stream_index = 1; 102 pkt->pts = c->audio_pos; 103 pkt->duration = c->packet[1] * 32; 104 c->audio_pos += pkt->duration; 105 c->get_next = 0; 106 return pkt->size; 107 } else 108 break; 109 } 110 if ((err = av_new_packet(pkt, c->size + 1)) < 0) 111 return err; 112 pkt->stream_index = 0; 113 c->get_next = 1; 114 memcpy(pkt->data, c->packet, pkt->size); 115 return pkt->size; 116} 117 118static int bmv_read_close(AVFormatContext *s) 119{ 120 BMVContext *c = s->priv_data; 121 122 av_freep(&c->packet); 123 124 return 0; 125} 126 127const AVInputFormat ff_bmv_demuxer = { 128 .name = "bmv", 129 .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV"), 130 .priv_data_size = sizeof(BMVContext), 131 .read_header = bmv_read_header, 132 .read_packet = bmv_read_packet, 133 .read_close = bmv_read_close, 134 .extensions = "bmv", 135}; 136