bmv.c
Go to the documentation of this file.
1 /*
2  * Discworld II BMV demuxer
3  * Copyright (c) 2011 Konstantin Shishkov.
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 #include "avformat.h"
23 #include "internal.h"
24 
25 enum BMVFlags {
26  BMV_NOP = 0,
30 
31  BMV_AUDIO = 0x20,
32 };
33 
34 typedef struct BMVContext {
35  uint8_t *packet;
36  int size;
37  int get_next;
38  int64_t audio_pos;
39 } BMVContext;
40 
42 {
43  AVStream *st, *ast;
44  BMVContext *c = s->priv_data;
45 
46  st = avformat_new_stream(s, 0);
47  if (!st)
48  return AVERROR(ENOMEM);
51  st->codec->width = 640;
52  st->codec->height = 429;
53  st->codec->pix_fmt = PIX_FMT_PAL8;
54  avpriv_set_pts_info(st, 16, 1, 12);
55  ast = avformat_new_stream(s, 0);
56  if (!ast)
57  return AVERROR(ENOMEM);
60  ast->codec->channels = 2;
61  ast->codec->sample_rate = 22050;
62  avpriv_set_pts_info(ast, 16, 1, 22050);
63 
64  c->get_next = 1;
65  c->audio_pos = 0;
66  return 0;
67 }
68 
70 {
71  BMVContext *c = s->priv_data;
72  int type;
73  void *tmp;
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  tmp = av_realloc(c->packet, c->size + 1);
87  if (!tmp)
88  return AVERROR(ENOMEM);
89  c->packet = tmp;
90  c->packet[0] = type;
91  if (avio_read(s->pb, c->packet + 1, c->size) != c->size)
92  return AVERROR(EIO);
93  if (type & BMV_AUDIO) {
94  int audio_size = c->packet[1] * 65 + 1;
95  if (audio_size >= c->size) {
96  av_log(s, AV_LOG_ERROR, "Reported audio size %d is bigger than packet size (%d)\n",
97  audio_size, c->size);
98  return AVERROR_INVALIDDATA;
99  }
100  if (av_new_packet(pkt, audio_size) < 0)
101  return AVERROR(ENOMEM);
102  memcpy(pkt->data, c->packet + 1, pkt->size);
103  pkt->stream_index = 1;
104  pkt->pts = c->audio_pos;
105  pkt->duration = c->packet[1] * 32;
106  c->audio_pos += pkt->duration;
107  c->get_next = 0;
108  return pkt->size;
109  } else
110  break;
111  }
112  if (av_new_packet(pkt, c->size + 1) < 0)
113  return AVERROR(ENOMEM);
114  pkt->stream_index = 0;
115  c->get_next = 1;
116  memcpy(pkt->data, c->packet, pkt->size);
117  return pkt->size;
118 }
119 
121 {
122  BMVContext *c = s->priv_data;
123 
124  av_freep(&c->packet);
125 
126  return 0;
127 }
128 
130  .name = "bmv",
131  .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV"),
132  .priv_data_size = sizeof(BMVContext),
136  .extensions = "bmv"
137 };
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
AVInputFormat ff_bmv_demuxer
Definition: bmv.c:129
Definition: bmv.c:27
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:60
Definition: bmv.c:29
int size
Definition: avcodec.h:909
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
uint8_t * packet
Definition: bmv.c:35
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:147
Format I/O context.
Definition: avformat.h:863
int get_next
Definition: bmv.c:37
static int bmv_read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: bmv.c:41
static int read_header(FFV1Context *f)
Definition: ffv1.c:1513
uint8_t * data
Definition: avcodec.h:908
Definition: bmv.c:30
#define AVERROR_EOF
End of file.
Definition: error.h:51
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:140
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:930
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:652
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
int64_t audio_pos
Definition: bmv.c:38
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
AVCodecContext * codec
codec context
Definition: avformat.h:623
static int64_t audio_size
Definition: avconv.c:133
static int bmv_read_close(AVFormatContext *s)
Definition: bmv.c:120
int width
picture width / height.
Definition: avcodec.h:1408
Definition: bmv.c:28
Stream structure.
Definition: avformat.h:620
enum AVMediaType codec_type
Definition: avcodec.h:1574
int sample_rate
samples per second
Definition: avcodec.h:1456
AVIOContext * pb
Definition: avformat.h:896
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:109
Definition: bmv.c:34
BMVFlags
Definition: bmv.c:26
int size
Definition: bmv.c:36
Main libavformat public API header.
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:75
int eof_reached
true if eof reached
Definition: avio.h:98
int channels
number of audio channels
Definition: avcodec.h:1457
static int bmv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: bmv.c:69
void * priv_data
Format private data.
Definition: avformat.h:883
Definition: bmv.c:35
struct BMVContext BMVContext
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:730
int stream_index
Definition: avcodec.h:910
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:901
enum CodecID codec_id
Definition: avcodec.h:1575