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 
24 #include "libavutil/channel_layout.h"
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     uint64_t audio_size_counter;
36     uint64_t audio_frame_size;
37     int audio_size_left;
38     int video_frame_size;
39 } MviDemuxContext;
40 
read_header(AVFormatContext *s)41 static int read_header(AVFormatContext *s)
42 {
43     MviDemuxContext *mvi = s->priv_data;
44     AVIOContext *pb = s->pb;
45     AVStream *ast, *vst;
46     unsigned int version, frames_count, msecs_per_frame, player_version;
47     int ret;
48     int audio_data_size;
49 
50     ast = avformat_new_stream(s, NULL);
51     if (!ast)
52         return AVERROR(ENOMEM);
53 
54     vst = avformat_new_stream(s, NULL);
55     if (!vst)
56         return AVERROR(ENOMEM);
57 
58     if ((ret = ff_alloc_extradata(vst->codecpar, 2)) < 0)
59         return ret;
60 
61     version                  = avio_r8(pb);
62     vst->codecpar->extradata[0] = avio_r8(pb);
63     vst->codecpar->extradata[1] = avio_r8(pb);
64     frames_count             = avio_rl32(pb);
65     msecs_per_frame          = avio_rl32(pb);
66     vst->codecpar->width        = avio_rl16(pb);
67     vst->codecpar->height       = avio_rl16(pb);
68     avio_r8(pb);
69     ast->codecpar->sample_rate  = avio_rl16(pb);
70     audio_data_size             = avio_rl32(pb);
71     avio_r8(pb);
72     player_version           = avio_rl32(pb);
73     avio_rl16(pb);
74     avio_r8(pb);
75 
76     if (frames_count == 0 || audio_data_size <= 0)
77         return AVERROR_INVALIDDATA;
78 
79     if (version != 7 || player_version > 213) {
80         av_log(s, AV_LOG_ERROR, "unhandled version (%d,%d)\n", version, player_version);
81         return AVERROR_INVALIDDATA;
82     }
83 
84     avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
85     ast->codecpar->codec_type      = AVMEDIA_TYPE_AUDIO;
86     ast->codecpar->codec_id        = AV_CODEC_ID_PCM_U8;
87     ast->codecpar->ch_layout       = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
88     ast->codecpar->bits_per_coded_sample = 8;
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);
93     vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
94     vst->codecpar->codec_id   = AV_CODEC_ID_MOTIONPIXELS;
95 
96     mvi->get_int = (vst->codecpar->width * (int64_t)vst->codecpar->height < (1 << 16)) ? avio_rl16 : avio_rl24;
97 
98     mvi->audio_frame_size   = ((uint64_t)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 (%d) or frames_count (%u)\n",
102                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    = audio_data_size;
108 
109     return 0;
110 }
111 
read_packet(AVFormatContext *s, AVPacket *pkt)112 static int read_packet(AVFormatContext *s, AVPacket *pkt)
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         if (mvi->audio_size_counter + 512 > UINT64_MAX - mvi->audio_frame_size ||
123             mvi->audio_size_counter + 512 + mvi->audio_frame_size >= ((uint64_t)INT32_MAX) << MVI_FRAC_BITS)
124             return AVERROR_INVALIDDATA;
125 
126         count = (mvi->audio_size_counter + mvi->audio_frame_size + 512) >> MVI_FRAC_BITS;
127         if (count > mvi->audio_size_left)
128             count = mvi->audio_size_left;
129         if ((int64_t)count << MVI_FRAC_BITS > INT_MAX)
130             return AVERROR_INVALIDDATA;
131         if ((ret = av_get_packet(pb, pkt, count)) < 0)
132             return ret;
133         pkt->stream_index = MVI_AUDIO_STREAM_INDEX;
134         mvi->audio_size_left -= count;
135         mvi->audio_size_counter += mvi->audio_frame_size - (count << MVI_FRAC_BITS);
136     } else {
137         if ((ret = av_get_packet(pb, pkt, mvi->video_frame_size)) < 0)
138             return ret;
139         pkt->stream_index = MVI_VIDEO_STREAM_INDEX;
140         mvi->video_frame_size = 0;
141     }
142     return 0;
143 }
144 
145 const AVInputFormat ff_mvi_demuxer = {
146     .name           = "mvi",
147     .long_name      = NULL_IF_CONFIG_SMALL("Motion Pixels MVI"),
148     .priv_data_size = sizeof(MviDemuxContext),
149     .read_header    = read_header,
150     .read_packet    = read_packet,
151     .extensions     = "mvi",
152 };
153