OSDN Git Service

ffmdec: set avio buffer to ffm->packet_size, avoid dirty reads
[coroid/ffmpeg_saccubus.git] / libavformat / mvi.c
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 "avformat.h"
23
24 #define MVI_FRAC_BITS 10
25
26 #define MVI_AUDIO_STREAM_INDEX 0
27 #define MVI_VIDEO_STREAM_INDEX 1
28
29 typedef struct MviDemuxContext {
30     unsigned int (*get_int)(AVIOContext *);
31     uint32_t audio_data_size;
32     uint64_t audio_size_counter;
33     uint64_t audio_frame_size;
34     int audio_size_left;
35     int video_frame_size;
36 } MviDemuxContext;
37
38 static int read_header(AVFormatContext *s, AVFormatParameters *ap)
39 {
40     MviDemuxContext *mvi = s->priv_data;
41     AVIOContext *pb = s->pb;
42     AVStream *ast, *vst;
43     unsigned int version, frames_count, msecs_per_frame, player_version;
44
45     ast = av_new_stream(s, 0);
46     if (!ast)
47         return AVERROR(ENOMEM);
48
49     vst = av_new_stream(s, 0);
50     if (!vst)
51         return AVERROR(ENOMEM);
52
53     vst->codec->extradata_size = 2;
54     vst->codec->extradata = av_mallocz(2 + FF_INPUT_BUFFER_PADDING_SIZE);
55
56     version                  = avio_r8(pb);
57     vst->codec->extradata[0] = avio_r8(pb);
58     vst->codec->extradata[1] = avio_r8(pb);
59     frames_count             = avio_rl32(pb);
60     msecs_per_frame          = avio_rl32(pb);
61     vst->codec->width        = avio_rl16(pb);
62     vst->codec->height       = avio_rl16(pb);
63     avio_r8(pb);
64     ast->codec->sample_rate  = avio_rl16(pb);
65     mvi->audio_data_size     = avio_rl32(pb);
66     avio_r8(pb);
67     player_version           = avio_rl32(pb);
68     avio_rl16(pb);
69     avio_r8(pb);
70
71     if (frames_count == 0 || mvi->audio_data_size == 0)
72         return AVERROR_INVALIDDATA;
73
74     if (version != 7 || player_version > 213) {
75         av_log(s, AV_LOG_ERROR, "unhandled version (%d,%d)\n", version, player_version);
76         return AVERROR_INVALIDDATA;
77     }
78
79     av_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
80     ast->codec->codec_type      = AVMEDIA_TYPE_AUDIO;
81     ast->codec->codec_id        = CODEC_ID_PCM_U8;
82     ast->codec->channels        = 1;
83     ast->codec->bits_per_coded_sample = 8;
84     ast->codec->bit_rate        = ast->codec->sample_rate * 8;
85
86     av_set_pts_info(vst, 64, msecs_per_frame, 1000000);
87     vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
88     vst->codec->codec_id   = CODEC_ID_MOTIONPIXELS;
89
90     mvi->get_int = (vst->codec->width * vst->codec->height < (1 << 16)) ? avio_rl16 : avio_rl24;
91
92     mvi->audio_frame_size   = ((uint64_t)mvi->audio_data_size << MVI_FRAC_BITS) / frames_count;
93     mvi->audio_size_counter = (ast->codec->sample_rate * 830 / mvi->audio_frame_size - 1) * mvi->audio_frame_size;
94     mvi->audio_size_left    = mvi->audio_data_size;
95
96     return 0;
97 }
98
99 static int read_packet(AVFormatContext *s, AVPacket *pkt)
100 {
101     int ret, count;
102     MviDemuxContext *mvi = s->priv_data;
103     AVIOContext *pb = s->pb;
104
105     if (mvi->video_frame_size == 0) {
106         mvi->video_frame_size = (mvi->get_int)(pb);
107         if (mvi->audio_size_left == 0)
108             return AVERROR(EIO);
109         count = (mvi->audio_size_counter + mvi->audio_frame_size + 512) >> MVI_FRAC_BITS;
110         if (count > mvi->audio_size_left)
111             count = mvi->audio_size_left;
112         if ((ret = av_get_packet(pb, pkt, count)) < 0)
113             return ret;
114         pkt->stream_index = MVI_AUDIO_STREAM_INDEX;
115         mvi->audio_size_left -= count;
116         mvi->audio_size_counter += mvi->audio_frame_size - (count << MVI_FRAC_BITS);
117     } else {
118         if ((ret = av_get_packet(pb, pkt, mvi->video_frame_size)) < 0)
119             return ret;
120         pkt->stream_index = MVI_VIDEO_STREAM_INDEX;
121         mvi->video_frame_size = 0;
122     }
123     return 0;
124 }
125
126 AVInputFormat ff_mvi_demuxer = {
127     .name           = "mvi",
128     .long_name      = NULL_IF_CONFIG_SMALL("Motion Pixels MVI format"),
129     .priv_data_size = sizeof(MviDemuxContext),
130     .read_header    = read_header,
131     .read_packet    = read_packet,
132     .extensions = "mvi"
133 };