FFmpeg  4.0
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 "internal.h"
27 
28 #define MVI_FRAC_BITS 10
29 
30 #define MVI_AUDIO_STREAM_INDEX 0
31 #define MVI_VIDEO_STREAM_INDEX 1
32 
33 typedef struct MviDemuxContext {
34  unsigned int (*get_int)(AVIOContext *);
35  uint32_t audio_data_size;
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 
49  ast = avformat_new_stream(s, NULL);
50  if (!ast)
51  return AVERROR(ENOMEM);
52 
53  vst = avformat_new_stream(s, NULL);
54  if (!vst)
55  return AVERROR(ENOMEM);
56 
57  if (ff_alloc_extradata(vst->codecpar, 2))
58  return AVERROR(ENOMEM);
59 
60  version = avio_r8(pb);
61  vst->codecpar->extradata[0] = avio_r8(pb);
62  vst->codecpar->extradata[1] = avio_r8(pb);
63  frames_count = avio_rl32(pb);
64  msecs_per_frame = avio_rl32(pb);
65  vst->codecpar->width = avio_rl16(pb);
66  vst->codecpar->height = avio_rl16(pb);
67  avio_r8(pb);
68  ast->codecpar->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->codecpar->sample_rate);
86  ast->codecpar->channels = 1;
89  ast->codecpar->bit_rate = ast->codecpar->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->codecpar->width * vst->codecpar->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 <= 1 << MVI_FRAC_BITS - 1) {
100  av_log(s, AV_LOG_ERROR,
101  "Invalid audio_data_size (%"PRIu32") or frames_count (%u)\n",
102  mvi->audio_data_size, frames_count);
103  return AVERROR_INVALIDDATA;
104  }
105 
106  mvi->audio_size_counter = (ast->codecpar->sample_rate * 830 / mvi->audio_frame_size - 1) * mvi->audio_frame_size;
107  mvi->audio_size_left = mvi->audio_data_size;
108 
109  return 0;
110 }
111 
113 {
114  int ret, count;
115  MviDemuxContext *mvi = s->priv_data;
116  AVIOContext *pb = s->pb;
117 
118  if (mvi->video_frame_size == 0) {
119  mvi->video_frame_size = (mvi->get_int)(pb);
120  if (mvi->audio_size_left == 0)
121  return AVERROR(EIO);
122  count = (mvi->audio_size_counter + mvi->audio_frame_size + 512) >> MVI_FRAC_BITS;
123  if (count > mvi->audio_size_left)
124  count = mvi->audio_size_left;
125  if ((ret = av_get_packet(pb, pkt, count)) < 0)
126  return ret;
128  mvi->audio_size_left -= count;
129  mvi->audio_size_counter += mvi->audio_frame_size - (count << MVI_FRAC_BITS);
130  } else {
131  if ((ret = av_get_packet(pb, pkt, mvi->video_frame_size)) < 0)
132  return ret;
134  mvi->video_frame_size = 0;
135  }
136  return 0;
137 }
138 
140  .name = "mvi",
141  .long_name = NULL_IF_CONFIG_SMALL("Motion Pixels MVI"),
142  .priv_data_size = sizeof(MviDemuxContext),
145  .extensions = "mvi",
146 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVInputFormat ff_mvi_demuxer
Definition: mvi.c:139
#define MVI_AUDIO_STREAM_INDEX
Definition: mvi.c:30
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:4811
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3884
int version
Definition: avisynth_c.h:766
static AVPacket pkt
Format I/O context.
Definition: avformat.h:1342
int width
Video only.
Definition: avcodec.h:3950
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4441
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:310
uint64_t channel_layout
Audio only.
Definition: avcodec.h:3986
#define av_log(a,...)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3913
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:770
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3880
unsigned int(* get_int)(AVIOContext *)
Definition: mvi.c:34
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:946
int audio_size_left
Definition: mvi.c:38
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:639
audio channel layout utility functions
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:3271
uint64_t audio_size_counter
Definition: mvi.c:36
#define MVI_VIDEO_STREAM_INDEX
Definition: mvi.c:31
Stream structure.
Definition: avformat.h:873
uint64_t audio_frame_size
Definition: mvi.c:37
AVIOContext * pb
I/O context.
Definition: avformat.h:1384
uint32_t audio_data_size
Definition: mvi.c:35
int video_frame_size
Definition: mvi.c:39
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
int sample_rate
Audio only.
Definition: avcodec.h:3994
static int read_header(AVFormatContext *s)
Definition: mvi.c:42
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:754
Main libavformat public API header.
int
#define MVI_FRAC_BITS
Definition: mvi.c:28
void * priv_data
Format private data.
Definition: avformat.h:1370
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3926
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3898
int channels
Audio only.
Definition: avcodec.h:3990
void INT64 INT64 count
Definition: avisynth_c.h:690
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:762
int stream_index
Definition: avcodec.h:1432
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:902
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:1407
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mvi.c:112