FFmpeg
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 
22 #include <inttypes.h>
23 
25 #include "avformat.h"
26 #include "demux.h"
27 #include "internal.h"
28 
29 #define MVI_FRAC_BITS 10
30 
31 #define MVI_AUDIO_STREAM_INDEX 0
32 #define MVI_VIDEO_STREAM_INDEX 1
33 
34 typedef struct MviDemuxContext {
35  unsigned int (*get_int)(AVIOContext *);
37  uint64_t audio_frame_size;
41 
43 {
44  MviDemuxContext *mvi = s->priv_data;
45  AVIOContext *pb = s->pb;
46  AVStream *ast, *vst;
47  unsigned int version, frames_count, msecs_per_frame, player_version;
48  int ret;
49  int audio_data_size;
50 
51  ast = avformat_new_stream(s, NULL);
52  if (!ast)
53  return AVERROR(ENOMEM);
54 
55  vst = avformat_new_stream(s, NULL);
56  if (!vst)
57  return AVERROR(ENOMEM);
58 
59  if ((ret = ff_alloc_extradata(vst->codecpar, 2)) < 0)
60  return ret;
61 
62  version = avio_r8(pb);
63  vst->codecpar->extradata[0] = avio_r8(pb);
64  vst->codecpar->extradata[1] = avio_r8(pb);
65  frames_count = avio_rl32(pb);
66  msecs_per_frame = avio_rl32(pb);
67  vst->codecpar->width = avio_rl16(pb);
68  vst->codecpar->height = avio_rl16(pb);
69  avio_r8(pb);
70  ast->codecpar->sample_rate = avio_rl16(pb);
71  audio_data_size = avio_rl32(pb);
72  avio_r8(pb);
73  player_version = avio_rl32(pb);
74  avio_rl16(pb);
75  avio_r8(pb);
76 
77  if (frames_count == 0 || audio_data_size <= 0)
78  return AVERROR_INVALIDDATA;
79 
80  if (version != 7 || player_version > 213) {
81  av_log(s, AV_LOG_ERROR, "unhandled version (%d,%d)\n", version, player_version);
82  return AVERROR_INVALIDDATA;
83  }
84 
85  avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
90  ast->codecpar->bit_rate = ast->codecpar->sample_rate * 8;
91 
92  avpriv_set_pts_info(vst, 64, msecs_per_frame, 1000000);
93  vst->avg_frame_rate = av_inv_q(vst->time_base);
96 
97  mvi->get_int = (vst->codecpar->width * (int64_t)vst->codecpar->height < (1 << 16)) ? avio_rl16 : avio_rl24;
98 
99  mvi->audio_frame_size = ((uint64_t)audio_data_size << MVI_FRAC_BITS) / frames_count;
100  if (mvi->audio_frame_size <= 1 << MVI_FRAC_BITS - 1) {
102  "Invalid audio_data_size (%d) or frames_count (%u)\n",
103  audio_data_size, frames_count);
104  return AVERROR_INVALIDDATA;
105  }
106 
107  mvi->audio_size_counter = (ast->codecpar->sample_rate * 830 / mvi->audio_frame_size - 1) * mvi->audio_frame_size;
108  mvi->audio_size_left = audio_data_size;
109 
110  return 0;
111 }
112 
114 {
115  int ret, count;
116  MviDemuxContext *mvi = s->priv_data;
117  AVIOContext *pb = s->pb;
118 
119  if (mvi->video_frame_size == 0) {
120  mvi->video_frame_size = (mvi->get_int)(pb);
121  if (mvi->audio_size_left == 0)
122  return AVERROR(EIO);
123  if (mvi->audio_size_counter + 512 > UINT64_MAX - mvi->audio_frame_size ||
124  mvi->audio_size_counter + 512 + mvi->audio_frame_size >= ((uint64_t)INT32_MAX) << MVI_FRAC_BITS)
125  return AVERROR_INVALIDDATA;
126 
127  count = (mvi->audio_size_counter + mvi->audio_frame_size + 512) >> MVI_FRAC_BITS;
128  if (count > mvi->audio_size_left)
129  count = mvi->audio_size_left;
130  if ((int64_t)count << MVI_FRAC_BITS > INT_MAX)
131  return AVERROR_INVALIDDATA;
132  if ((ret = av_get_packet(pb, pkt, count)) < 0)
133  return ret;
135  mvi->audio_size_left -= count;
136  mvi->audio_size_counter += mvi->audio_frame_size - (count << MVI_FRAC_BITS);
137  } else {
138  if ((ret = av_get_packet(pb, pkt, mvi->video_frame_size)) < 0)
139  return ret;
141  mvi->video_frame_size = 0;
142  }
143  return 0;
144 }
145 
147  .p.name = "mvi",
148  .p.long_name = NULL_IF_CONFIG_SMALL("Motion Pixels MVI"),
149  .p.extensions = "mvi",
150  .priv_data_size = sizeof(MviDemuxContext),
153 };
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
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
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
MviDemuxContext::audio_frame_size
uint64_t audio_frame_size
Definition: mvi.c:37
int64_t
long long int64_t
Definition: coverity.c:34
MVI_AUDIO_STREAM_INDEX
#define MVI_AUDIO_STREAM_INDEX
Definition: mvi.c:31
read_packet
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mvi.c:113
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:837
MVI_FRAC_BITS
#define MVI_FRAC_BITS
Definition: mvi.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
MviDemuxContext::video_frame_size
int video_frame_size
Definition: mvi.c:39
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
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
s
#define s(width, name)
Definition: cbs_vp9.c:198
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
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
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:787
NULL
#define NULL
Definition: coverity.c:32
MviDemuxContext
Definition: mvi.c:34
AV_CODEC_ID_MOTIONPIXELS
@ AV_CODEC_ID_MOTIONPIXELS
Definition: codec_id.h:171
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
ff_mvi_demuxer
const FFInputFormat ff_mvi_demuxer
Definition: mvi.c:146
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
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
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
read_header
static int read_header(AVFormatContext *s)
Definition: mvi.c:42
version
version
Definition: libkvazaar.c:321
avio_rl24
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:722
AVCodecParameters::height
int height
Definition: codec_par.h:135
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
demux.h
av_get_packet
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.
Definition: utils.c:91
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avformat.h
channel_layout.h
MviDemuxContext::audio_size_left
int audio_size_left
Definition: mvi.c:38
AVPacket::stream_index
int stream_index
Definition: packet.h:541
MviDemuxContext::audio_size_counter
uint64_t audio_size_counter
Definition: mvi.c:36
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:334
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:386
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
FFInputFormat
Definition: demux.h:37
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
MVI_VIDEO_STREAM_INDEX
#define MVI_VIDEO_STREAM_INDEX
Definition: mvi.c:32
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_alloc_extradata
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:227
MviDemuxContext::get_int
unsigned int(* get_int)(AVIOContext *)
Definition: mvi.c:35