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 "libavutil/mem.h"
24 #include "avformat.h"
25 #include "demux.h"
26 #include "internal.h"
27 
28 enum BMVFlags {
29  BMV_NOP = 0,
33 
34  BMV_AUDIO = 0x20,
35 };
36 
37 typedef struct BMVContext {
38  uint8_t *packet;
39  int size;
40  int get_next;
42 } BMVContext;
43 
45 {
46  AVStream *st, *ast;
47  BMVContext *c = s->priv_data;
48 
49  st = avformat_new_stream(s, 0);
50  if (!st)
51  return AVERROR(ENOMEM);
54  st->codecpar->width = 640;
55  st->codecpar->height = 429;
57  avpriv_set_pts_info(st, 16, 1, 12);
58  ast = avformat_new_stream(s, 0);
59  if (!ast)
60  return AVERROR(ENOMEM);
64  ast->codecpar->sample_rate = 22050;
65  avpriv_set_pts_info(ast, 16, 1, 22050);
66 
67  c->get_next = 1;
68  c->audio_pos = 0;
69  return 0;
70 }
71 
73 {
74  BMVContext *c = s->priv_data;
75  int type, err;
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  if ((err = av_reallocp(&c->packet, c->size + 1)) < 0)
89  return err;
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 ((err = av_new_packet(pkt, audio_size)) < 0)
101  return err;
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 ((err = av_new_packet(pkt, c->size + 1)) < 0)
113  return err;
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  .p.name = "bmv",
131  .p.long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV"),
132  .p.extensions = "bmv",
133  .priv_data_size = sizeof(BMVContext),
137 };
BMV_AUDIO
@ BMV_AUDIO
Definition: bmv.c:34
bmv_read_packet
static int bmv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: bmv.c:72
BMV_DELTA
@ BMV_DELTA
Definition: bmv.c:31
AVERROR
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:387
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
int64_t
long long int64_t
Definition: coverity.c:34
BMVContext::size
int size
Definition: bmv.c:39
BMV_END
@ BMV_END
Definition: bmv.c:30
AVPacket::data
uint8_t * data
Definition: packet.h:539
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
BMV_NOP
@ BMV_NOP
Definition: bmv.c:29
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:862
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
BMVContext::packet
uint8_t * packet
Definition: bmv.c:38
BMVFlags
BMVFlags
Definition: bmvvideo.c:30
BMVContext::get_next
int get_next
Definition: bmv.c:40
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
bmv_read_header
static int bmv_read_header(AVFormatContext *s)
Definition: bmv.c:44
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: packet.c:98
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
BMV_INTRA
@ BMV_INTRA
Definition: bmv.c:32
BMVContext::audio_pos
int64_t audio_pos
Definition: bmv.c:41
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
c
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
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVPacket::size
int size
Definition: packet.h:540
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:311
av_reallocp
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:188
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
avio_rl24
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:722
AVCodecParameters::height
int height
Definition: codec_par.h:135
AV_CODEC_ID_BMV_AUDIO
@ AV_CODEC_ID_BMV_AUDIO
Definition: codec_id.h:501
demux.h
bmv_read_close
static int bmv_read_close(AVFormatContext *s)
Definition: bmv.c:120
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:84
AVStream
Stream structure.
Definition: avformat.h:748
AV_CODEC_ID_BMV_VIDEO
@ AV_CODEC_ID_BMV_VIDEO
Definition: codec_id.h:206
avformat.h
channel_layout.h
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
AVPacket::stream_index
int stream_index
Definition: packet.h:541
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
AVCodecParameters::format
int format
Definition: codec_par.h:92
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
BMVContext
Definition: bmv.c:37
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
FFInputFormat
Definition: demux.h:37
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_bmv_demuxer
const FFInputFormat ff_bmv_demuxer
Definition: bmv.c:129