Line | Branch | Exec | Source |
---|---|---|---|
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 "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 *); | ||
36 | uint64_t audio_size_counter; | ||
37 | uint64_t audio_frame_size; | ||
38 | int audio_size_left; | ||
39 | int video_frame_size; | ||
40 | } MviDemuxContext; | ||
41 | |||
42 | 1 | static int read_header(AVFormatContext *s) | |
43 | { | ||
44 | 1 | MviDemuxContext *mvi = s->priv_data; | |
45 | 1 | 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 | 1 | ast = avformat_new_stream(s, NULL); | |
52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!ast) |
53 | ✗ | return AVERROR(ENOMEM); | |
54 | |||
55 | 1 | vst = avformat_new_stream(s, NULL); | |
56 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!vst) |
57 | ✗ | return AVERROR(ENOMEM); | |
58 | |||
59 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = ff_alloc_extradata(vst->codecpar, 2)) < 0) |
60 | ✗ | return ret; | |
61 | |||
62 | 1 | version = avio_r8(pb); | |
63 | 1 | vst->codecpar->extradata[0] = avio_r8(pb); | |
64 | 1 | vst->codecpar->extradata[1] = avio_r8(pb); | |
65 | 1 | frames_count = avio_rl32(pb); | |
66 | 1 | msecs_per_frame = avio_rl32(pb); | |
67 | 1 | vst->codecpar->width = avio_rl16(pb); | |
68 | 1 | vst->codecpar->height = avio_rl16(pb); | |
69 | 1 | avio_r8(pb); | |
70 | 1 | ast->codecpar->sample_rate = avio_rl16(pb); | |
71 | 1 | audio_data_size = avio_rl32(pb); | |
72 | 1 | avio_r8(pb); | |
73 | 1 | player_version = avio_rl32(pb); | |
74 | 1 | avio_rl16(pb); | |
75 | 1 | avio_r8(pb); | |
76 | |||
77 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (frames_count == 0 || audio_data_size <= 0) |
78 | ✗ | return AVERROR_INVALIDDATA; | |
79 | |||
80 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | 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 | 1 | avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate); | |
86 | 1 | ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
87 | 1 | ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8; | |
88 | 1 | ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; | |
89 | 1 | ast->codecpar->bits_per_coded_sample = 8; | |
90 | 1 | ast->codecpar->bit_rate = ast->codecpar->sample_rate * 8; | |
91 | |||
92 | 1 | avpriv_set_pts_info(vst, 64, msecs_per_frame, 1000000); | |
93 | 1 | vst->avg_frame_rate = av_inv_q(vst->time_base); | |
94 | 1 | vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
95 | 1 | vst->codecpar->codec_id = AV_CODEC_ID_MOTIONPIXELS; | |
96 | |||
97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | mvi->get_int = (vst->codecpar->width * (int64_t)vst->codecpar->height < (1 << 16)) ? avio_rl16 : avio_rl24; |
98 | |||
99 | 1 | mvi->audio_frame_size = ((uint64_t)audio_data_size << MVI_FRAC_BITS) / frames_count; | |
100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (mvi->audio_frame_size <= 1 << MVI_FRAC_BITS - 1) { |
101 | ✗ | av_log(s, AV_LOG_ERROR, | |
102 | "Invalid audio_data_size (%d) or frames_count (%u)\n", | ||
103 | audio_data_size, frames_count); | ||
104 | ✗ | return AVERROR_INVALIDDATA; | |
105 | } | ||
106 | |||
107 | 1 | mvi->audio_size_counter = (ast->codecpar->sample_rate * 830 / mvi->audio_frame_size - 1) * mvi->audio_frame_size; | |
108 | 1 | mvi->audio_size_left = audio_data_size; | |
109 | |||
110 | 1 | return 0; | |
111 | } | ||
112 | |||
113 | 225 | static int read_packet(AVFormatContext *s, AVPacket *pkt) | |
114 | { | ||
115 | int ret, count; | ||
116 | 225 | MviDemuxContext *mvi = s->priv_data; | |
117 | 225 | AVIOContext *pb = s->pb; | |
118 | |||
119 |
2/2✓ Branch 0 taken 113 times.
✓ Branch 1 taken 112 times.
|
225 | if (mvi->video_frame_size == 0) { |
120 | 113 | mvi->video_frame_size = (mvi->get_int)(pb); | |
121 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
|
113 | if (mvi->audio_size_left == 0) |
122 | ✗ | return AVERROR(EIO); | |
123 |
1/2✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
|
113 | if (mvi->audio_size_counter + 512 > UINT64_MAX - mvi->audio_frame_size || |
124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
|
113 | mvi->audio_size_counter + 512 + mvi->audio_frame_size >= ((uint64_t)INT32_MAX) << MVI_FRAC_BITS) |
125 | ✗ | return AVERROR_INVALIDDATA; | |
126 | |||
127 | 113 | count = (mvi->audio_size_counter + mvi->audio_frame_size + 512) >> MVI_FRAC_BITS; | |
128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
|
113 | if (count > mvi->audio_size_left) |
129 | ✗ | count = mvi->audio_size_left; | |
130 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
|
113 | if ((int64_t)count << MVI_FRAC_BITS > INT_MAX) |
131 | ✗ | return AVERROR_INVALIDDATA; | |
132 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 112 times.
|
113 | if ((ret = av_get_packet(pb, pkt, count)) < 0) |
133 | 1 | return ret; | |
134 | 112 | pkt->stream_index = MVI_AUDIO_STREAM_INDEX; | |
135 | 112 | mvi->audio_size_left -= count; | |
136 | 112 | mvi->audio_size_counter += mvi->audio_frame_size - (count << MVI_FRAC_BITS); | |
137 | } else { | ||
138 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
|
112 | if ((ret = av_get_packet(pb, pkt, mvi->video_frame_size)) < 0) |
139 | ✗ | return ret; | |
140 | 112 | pkt->stream_index = MVI_VIDEO_STREAM_INDEX; | |
141 | 112 | mvi->video_frame_size = 0; | |
142 | } | ||
143 | 224 | return 0; | |
144 | } | ||
145 | |||
146 | const FFInputFormat ff_mvi_demuxer = { | ||
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), | ||
151 | .read_header = read_header, | ||
152 | .read_packet = read_packet, | ||
153 | }; | ||
154 |