libavformat/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 
23 #include "avformat.h"
24 #include "internal.h"
25 
26 enum BMVFlags {
27  BMV_NOP = 0,
31 
32  BMV_AUDIO = 0x20,
33 };
34 
35 typedef struct BMVContext {
37  int size;
38  int get_next;
39  int64_t audio_pos;
40 } BMVContext;
41 
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);
52  st->codec->width = 640;
53  st->codec->height = 429;
55  avpriv_set_pts_info(st, 16, 1, 12);
56  ast = avformat_new_stream(s, 0);
57  if (!ast)
58  return AVERROR(ENOMEM);
61  ast->codec->channels = 2;
63  ast->codec->sample_rate = 22050;
64  avpriv_set_pts_info(ast, 16, 1, 22050);
65 
66  c->get_next = 1;
67  c->audio_pos = 0;
68  return 0;
69 }
70 
72 {
73  BMVContext *c = s->priv_data;
74  int type;
75  void *tmp;
76 
77  while (c->get_next) {
78  if (s->pb->eof_reached)
79  return AVERROR_EOF;
80  type = avio_r8(s->pb);
81  if (type == BMV_NOP)
82  continue;
83  if (type == BMV_END)
84  return AVERROR_EOF;
85  c->size = avio_rl24(s->pb);
86  if (!c->size)
87  return AVERROR_INVALIDDATA;
88  tmp = av_realloc(c->packet, c->size + 1);
89  if (!tmp)
90  return AVERROR(ENOMEM);
91  c->packet = tmp;
92  c->packet[0] = type;
93  if (avio_read(s->pb, c->packet + 1, c->size) != c->size)
94  return AVERROR(EIO);
95  if (type & BMV_AUDIO) {
96  int audio_size = c->packet[1] * 65 + 1;
97  if (audio_size >= c->size) {
98  av_log(s, AV_LOG_ERROR, "Reported audio size %d is bigger than packet size (%d)\n",
99  audio_size, c->size);
100  return AVERROR_INVALIDDATA;
101  }
102  if (av_new_packet(pkt, audio_size) < 0)
103  return AVERROR(ENOMEM);
104  memcpy(pkt->data, c->packet + 1, pkt->size);
105  pkt->stream_index = 1;
106  pkt->pts = c->audio_pos;
107  pkt->duration = c->packet[1] * 32;
108  c->audio_pos += pkt->duration;
109  c->get_next = 0;
110  return pkt->size;
111  } else
112  break;
113  }
114  if (av_new_packet(pkt, c->size + 1) < 0)
115  return AVERROR(ENOMEM);
116  pkt->stream_index = 0;
117  c->get_next = 1;
118  memcpy(pkt->data, c->packet, pkt->size);
119  return pkt->size;
120 }
121 
123 {
124  BMVContext *c = s->priv_data;
125 
126  av_freep(&c->packet);
127 
128  return 0;
129 }
130 
132  .name = "bmv",
133  .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV"),
134  .priv_data_size = sizeof(BMVContext),
138  .extensions = "bmv",
139 };
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVInputFormat ff_bmv_demuxer
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
static int64_t audio_size
Definition: ffmpeg.c:126
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:141
#define AV_CH_LAYOUT_STEREO
uint8_t * packet
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:198
Format I/O context.
Definition: avformat.h:944
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:79
static AVPacket pkt
Definition: demuxing.c:56
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint8_t * data
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:73
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static int bmv_read_header(AVFormatContext *s)
int64_t audio_pos
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
uint64_t channel_layout
Audio channel layout.
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:469
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
audio channel layout utility functions
static int bmv_read_close(AVFormatContext *s)
int width
picture width / height.
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:517
Stream structure.
Definition: avformat.h:643
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
BMVFlags
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
#define type
Main libavformat public API header.
static double c[64]
int eof_reached
true if eof reached
Definition: avio.h:96
int channels
number of audio channels
static int bmv_read_packet(AVFormatContext *s, AVPacket *pkt)
void * priv_data
Format private data.
Definition: avformat.h:964
struct BMVContext BMVContext
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:571
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...