mvi.c
Go to the documentation of this file.
1 /*
2  * Motion Pixels MVI Demuxer
3  * Copyright (c) 2008 Gregory Montoir (cyx@users.sourceforge.net)
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 #define MVI_FRAC_BITS 10
27 
28 #define MVI_AUDIO_STREAM_INDEX 0
29 #define MVI_VIDEO_STREAM_INDEX 1
30 
31 typedef struct MviDemuxContext {
32  unsigned int (*get_int)(AVIOContext *);
33  uint32_t audio_data_size;
35  uint64_t audio_frame_size;
39 
41 {
42  MviDemuxContext *mvi = s->priv_data;
43  AVIOContext *pb = s->pb;
44  AVStream *ast, *vst;
45  unsigned int version, frames_count, msecs_per_frame, player_version;
46 
47  ast = avformat_new_stream(s, NULL);
48  if (!ast)
49  return AVERROR(ENOMEM);
50 
51  vst = avformat_new_stream(s, NULL);
52  if (!vst)
53  return AVERROR(ENOMEM);
54 
55  vst->codec->extradata_size = 2;
57  if (!vst->codec->extradata)
58  return AVERROR(ENOMEM);
59 
60  version = avio_r8(pb);
61  vst->codec->extradata[0] = avio_r8(pb);
62  vst->codec->extradata[1] = avio_r8(pb);
63  frames_count = avio_rl32(pb);
64  msecs_per_frame = avio_rl32(pb);
65  vst->codec->width = avio_rl16(pb);
66  vst->codec->height = avio_rl16(pb);
67  avio_r8(pb);
68  ast->codec->sample_rate = avio_rl16(pb);
69  mvi->audio_data_size = avio_rl32(pb);
70  avio_r8(pb);
71  player_version = avio_rl32(pb);
72  avio_rl16(pb);
73  avio_r8(pb);
74 
75  if (frames_count == 0 || mvi->audio_data_size == 0)
76  return AVERROR_INVALIDDATA;
77 
78  if (version != 7 || player_version > 213) {
79  av_log(s, AV_LOG_ERROR, "unhandled version (%d,%d)\n", version, player_version);
80  return AVERROR_INVALIDDATA;
81  }
82 
83  avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
86  ast->codec->channels = 1;
88  ast->codec->bits_per_coded_sample = 8;
89  ast->codec->bit_rate = ast->codec->sample_rate * 8;
90 
91  avpriv_set_pts_info(vst, 64, msecs_per_frame, 1000000);
92  vst->avg_frame_rate = av_inv_q(vst->time_base);
95 
96  mvi->get_int = (vst->codec->width * vst->codec->height < (1 << 16)) ? avio_rl16 : avio_rl24;
97 
98  mvi->audio_frame_size = ((uint64_t)mvi->audio_data_size << MVI_FRAC_BITS) / frames_count;
99  if (!mvi->audio_frame_size) {
100  av_log(s, AV_LOG_ERROR, "audio_frame_size is 0\n");
101  return AVERROR_INVALIDDATA;
102  }
103  mvi->audio_size_counter = (ast->codec->sample_rate * 830 / mvi->audio_frame_size - 1) * mvi->audio_frame_size;
104  mvi->audio_size_left = mvi->audio_data_size;
105 
106  return 0;
107 }
108 
110 {
111  int ret, count;
112  MviDemuxContext *mvi = s->priv_data;
113  AVIOContext *pb = s->pb;
114 
115  if (mvi->video_frame_size == 0) {
116  mvi->video_frame_size = (mvi->get_int)(pb);
117  if (mvi->audio_size_left == 0)
118  return AVERROR(EIO);
119  count = (mvi->audio_size_counter + mvi->audio_frame_size + 512) >> MVI_FRAC_BITS;
120  if (count > mvi->audio_size_left)
121  count = mvi->audio_size_left;
122  if ((ret = av_get_packet(pb, pkt, count)) < 0)
123  return ret;
125  mvi->audio_size_left -= count;
126  mvi->audio_size_counter += mvi->audio_frame_size - (count << MVI_FRAC_BITS);
127  } else {
128  if ((ret = av_get_packet(pb, pkt, mvi->video_frame_size)) < 0)
129  return ret;
131  mvi->video_frame_size = 0;
132  }
133  return 0;
134 }
135 
137  .name = "mvi",
138  .long_name = NULL_IF_CONFIG_SMALL("Motion Pixels MVI"),
139  .priv_data_size = sizeof(MviDemuxContext),
142  .extensions = "mvi",
143 };
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVInputFormat ff_mvi_demuxer
Definition: mvi.c:136
#define MVI_AUDIO_STREAM_INDEX
Definition: mvi.c:28
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.
int version
Definition: avisynth_c.h:666
Format I/O context.
Definition: avformat.h:944
static AVPacket pkt
Definition: demuxing.c:56
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:579
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
unsigned int(* get_int)(AVIOContext *)
Definition: mvi.c:32
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:716
int audio_size_left
Definition: mvi.c:36
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
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int bit_rate
the average bitrate
audio channel layout utility functions
uint64_t audio_size_counter
Definition: mvi.c:34
ret
Definition: avfilter.c:821
int width
picture width / height.
#define MVI_VIDEO_STREAM_INDEX
Definition: mvi.c:29
Stream structure.
Definition: avformat.h:643
uint64_t audio_frame_size
Definition: mvi.c:35
NULL
Definition: eval.c:55
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
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
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:122
uint32_t audio_data_size
Definition: mvi.c:33
int video_frame_size
Definition: mvi.c:37
static int read_header(AVFormatContext *s)
Definition: mvi.c:40
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:563
Main libavformat public API header.
#define MVI_FRAC_BITS
Definition: mvi.c:26
struct MviDemuxContext MviDemuxContext
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
void INT64 INT64 count
Definition: avisynth_c.h:594
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
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:679
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mvi.c:109