FFmpeg
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 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 
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->codecpar->width = 640;
53  st->codecpar->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->codecpar->channels = 2;
63  ast->codecpar->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, err;
75 
76  while (c->get_next) {
77  if (s->pb->eof_reached)
78  return AVERROR_EOF;
79  type = avio_r8(s->pb);
80  if (type == BMV_NOP)
81  continue;
82  if (type == BMV_END)
83  return AVERROR_EOF;
84  c->size = avio_rl24(s->pb);
85  if (!c->size)
86  return AVERROR_INVALIDDATA;
87  if ((err = av_reallocp(&c->packet, c->size + 1)) < 0)
88  return err;
89  c->packet[0] = type;
90  if (avio_read(s->pb, c->packet + 1, c->size) != c->size)
91  return AVERROR(EIO);
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",
96  audio_size, c->size);
97  return AVERROR_INVALIDDATA;
98  }
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;
106  c->get_next = 0;
107  return pkt->size;
108  } else
109  break;
110  }
111  if (av_new_packet(pkt, c->size + 1) < 0)
112  return AVERROR(ENOMEM);
113  pkt->stream_index = 0;
114  c->get_next = 1;
115  memcpy(pkt->data, c->packet, pkt->size);
116  return pkt->size;
117 }
118 
120 {
121  BMVContext *c = s->priv_data;
122 
123  av_freep(&c->packet);
124 
125  return 0;
126 }
127 
129  .name = "bmv",
130  .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV"),
131  .priv_data_size = sizeof(BMVContext),
135  .extensions = "bmv",
136 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int bmv_read_close(AVFormatContext *s)
Definition: bmv.c:119
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.
Definition: utils.c:4926
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3968
int size
Definition: avcodec.h:1481
static int bmv_read_header(AVFormatContext *s)
Definition: bmv.c:42
GLint GLenum type
Definition: opengl_enc.c:104
static AVPacket pkt
#define AV_CH_LAYOUT_STEREO
uint8_t * packet
Definition: bmv.c:36
Format I/O context.
Definition: avformat.h:1358
int get_next
Definition: bmv.c:38
BMVFlags
Definition: bmvvideo.c:29
uint8_t
int width
Video only.
Definition: avcodec.h:4034
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
Definition: bmv.c:28
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1498
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4499
uint8_t * data
Definition: avcodec.h:1480
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
Definition: bmv.c:30
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4070
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:650
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
AVInputFormat ff_bmv_demuxer
Definition: bmv.c:128
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
int64_t audio_pos
Definition: bmv.c:39
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3964
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:641
audio channel layout utility functions
#define s(width, name)
Definition: cbs_vp9.c:257
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
Stream structure.
Definition: avformat.h:881
Definition: bmv.c:27
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:163
AVIOContext * pb
I/O context.
Definition: avformat.h:1400
Definition: bmv.c:29
Definition: bmv.c:35
int size
Definition: bmv.c:37
int sample_rate
Audio only.
Definition: avcodec.h:4078
Main libavformat public API header.
int eof_reached
true if was unable to read due to error or eof
Definition: avio.h:239
void * priv_data
Format private data.
Definition: avformat.h:1386
int channels
Audio only.
Definition: avcodec.h:4074
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:654
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1028
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:764
int stream_index
Definition: avcodec.h:1482
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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.Frame references ownership and permissions
This structure stores compressed data.
Definition: avcodec.h:1457
static int bmv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: bmv.c:71
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1473
Definition: bmv.c:32