| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * mtv demuxer | ||
| 3 | * Copyright (c) 2006 Reynaldo H. Verdejo Pinochet | ||
| 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 | /** | ||
| 23 | * @file | ||
| 24 | * MTV demuxer. | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include "libavutil/intreadwrite.h" | ||
| 28 | #include "libavutil/mem.h" | ||
| 29 | #include "avformat.h" | ||
| 30 | #include "demux.h" | ||
| 31 | #include "internal.h" | ||
| 32 | |||
| 33 | #define MTV_ASUBCHUNK_DATA_SIZE 500 | ||
| 34 | #define MTV_HEADER_SIZE 512 | ||
| 35 | #define MTV_AUDIO_PADDING_SIZE 12 | ||
| 36 | #define MTV_IMAGE_DEFAULT_BPP 16 | ||
| 37 | #define MTV_AUDIO_SAMPLING_RATE 44100 | ||
| 38 | |||
| 39 | typedef struct MTVDemuxContext { | ||
| 40 | |||
| 41 | unsigned int file_size; ///< filesize, not always right | ||
| 42 | unsigned int segments; ///< number of 512 byte segments | ||
| 43 | unsigned int audio_identifier; ///< 'MP3' on all files I have seen | ||
| 44 | unsigned int audio_br; ///< bitrate of audio channel (mp3) | ||
| 45 | unsigned int img_colorfmt; ///< frame colorfmt rgb 565/555 | ||
| 46 | unsigned int img_bpp; ///< frame bits per pixel | ||
| 47 | unsigned int img_width; | ||
| 48 | unsigned int img_height; | ||
| 49 | unsigned int img_segment_size; ///< size of image segment | ||
| 50 | unsigned int video_fps; | ||
| 51 | unsigned int full_segment_size; | ||
| 52 | |||
| 53 | } MTVDemuxContext; | ||
| 54 | |||
| 55 | 7467 | static int mtv_probe(const AVProbeData *p) | |
| 56 | { | ||
| 57 | /* we need at least 57 bytes from the header | ||
| 58 | * to try parsing all required fields | ||
| 59 | */ | ||
| 60 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7464 times.
|
7467 | if (p->buf_size < 57) |
| 61 | 3 | return 0; | |
| 62 | |||
| 63 | /* Magic is 'AMV' */ | ||
| 64 |
5/6✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7451 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
7464 | if (*p->buf != 'A' || *(p->buf + 1) != 'M' || *(p->buf + 2) != 'V') |
| 65 | 7463 | return 0; | |
| 66 | |||
| 67 | /* Audio magic is always MP3 */ | ||
| 68 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
1 | if (p->buf[43] != 'M' || p->buf[44] != 'P' || p->buf[45] != '3') |
| 69 | ✗ | return 0; | |
| 70 | |||
| 71 | /* Check for nonzero in bpp and (width|height) header fields */ | ||
| 72 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if(!(p->buf[51] && AV_RL16(&p->buf[52]) | AV_RL16(&p->buf[54]))) |
| 73 | ✗ | return 0; | |
| 74 | |||
| 75 | /* If width or height are 0 then imagesize header field should not */ | ||
| 76 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54])) |
| 77 | { | ||
| 78 | ✗ | if(!!AV_RL16(&p->buf[56])) | |
| 79 | ✗ | return AVPROBE_SCORE_EXTENSION; | |
| 80 | else | ||
| 81 | ✗ | return 0; | |
| 82 | } | ||
| 83 | |||
| 84 | /* Image bpp is not an absolutely required | ||
| 85 | * field as we latter claim it should be 16 | ||
| 86 | * no matter what. All samples in the wild | ||
| 87 | * are RGB565/555. | ||
| 88 | */ | ||
| 89 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if(p->buf[51] != MTV_IMAGE_DEFAULT_BPP) |
| 90 | ✗ | return AVPROBE_SCORE_EXTENSION / 2; | |
| 91 | |||
| 92 | /* We had enough data to parse header values | ||
| 93 | * but we expect to be able to get 512 bytes | ||
| 94 | * of header to be sure. | ||
| 95 | */ | ||
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (p->buf_size < MTV_HEADER_SIZE) |
| 97 | ✗ | return AVPROBE_SCORE_EXTENSION; | |
| 98 | |||
| 99 | 1 | return AVPROBE_SCORE_MAX; | |
| 100 | } | ||
| 101 | |||
| 102 | 1 | static int mtv_read_header(AVFormatContext *s) | |
| 103 | { | ||
| 104 | 1 | MTVDemuxContext *mtv = s->priv_data; | |
| 105 | 1 | AVIOContext *pb = s->pb; | |
| 106 | AVStream *st; | ||
| 107 | unsigned int audio_subsegments; | ||
| 108 | int64_t ret64; | ||
| 109 | |||
| 110 | 1 | avio_skip(pb, 3); | |
| 111 | 1 | mtv->file_size = avio_rl32(pb); | |
| 112 | 1 | mtv->segments = avio_rl32(pb); | |
| 113 | 1 | avio_skip(pb, 32); | |
| 114 | 1 | mtv->audio_identifier = avio_rl24(pb); | |
| 115 | 1 | mtv->audio_br = avio_rl16(pb); | |
| 116 | 1 | mtv->img_colorfmt = avio_rl24(pb); | |
| 117 | 1 | mtv->img_bpp = avio_r8(pb); | |
| 118 | 1 | mtv->img_width = avio_rl16(pb); | |
| 119 | 1 | mtv->img_height = avio_rl16(pb); | |
| 120 | 1 | mtv->img_segment_size = avio_rl16(pb); | |
| 121 | |||
| 122 | /* Assume 16bpp even if claimed otherwise. | ||
| 123 | * We know its going to be RGBG565/555 anyway | ||
| 124 | */ | ||
| 125 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (mtv->img_bpp != MTV_IMAGE_DEFAULT_BPP) { |
| 126 | ✗ | av_log (s, AV_LOG_WARNING, "Header claims %dbpp (!= 16). Ignoring\n", | |
| 127 | mtv->img_bpp); | ||
| 128 | ✗ | mtv->img_bpp = MTV_IMAGE_DEFAULT_BPP; | |
| 129 | } | ||
| 130 | |||
| 131 | /* Calculate width and height if missing from header */ | ||
| 132 | |||
| 133 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
1 | if (!mtv->img_width && mtv->img_height > 0 && mtv->img_bpp >= 8) |
| 134 | ✗ | mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3) | |
| 135 | ✗ | / mtv->img_height; | |
| 136 | |||
| 137 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
1 | if (!mtv->img_height && mtv->img_width > 0 && mtv->img_bpp >= 8) |
| 138 | ✗ | mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3) | |
| 139 | ✗ | / mtv->img_width; | |
| 140 | |||
| 141 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
1 | if(!mtv->img_height || !mtv->img_width || !mtv->img_segment_size){ |
| 142 | ✗ | av_log(s, AV_LOG_ERROR, "width or height or segment_size is invalid and I cannot calculate them from other information\n"); | |
| 143 | ✗ | return AVERROR_INVALIDDATA; | |
| 144 | } | ||
| 145 | |||
| 146 | 1 | avio_skip(pb, 4); | |
| 147 | 1 | audio_subsegments = avio_rl16(pb); | |
| 148 | |||
| 149 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (audio_subsegments == 0) { |
| 150 | ✗ | avpriv_request_sample(s, "MTV files without audio"); | |
| 151 | ✗ | return AVERROR_PATCHWELCOME; | |
| 152 | } | ||
| 153 | |||
| 154 | 1 | mtv->full_segment_size = | |
| 155 | 1 | audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) + | |
| 156 | 1 | mtv->img_segment_size; | |
| 157 | 1 | mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments; | |
| 158 | |||
| 159 | // FIXME Add sanity check here | ||
| 160 | |||
| 161 | // all systems go! init decoders | ||
| 162 | |||
| 163 | // video - raw rgb565 | ||
| 164 | |||
| 165 | 1 | st = avformat_new_stream(s, NULL); | |
| 166 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if(!st) |
| 167 | ✗ | return AVERROR(ENOMEM); | |
| 168 | |||
| 169 | 1 | avpriv_set_pts_info(st, 64, 1, mtv->video_fps); | |
| 170 | 1 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 171 | 1 | st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO; | |
| 172 | 1 | st->codecpar->format = AV_PIX_FMT_RGB565BE; | |
| 173 | 1 | st->codecpar->width = mtv->img_width; | |
| 174 | 1 | st->codecpar->height = mtv->img_height; | |
| 175 | 1 | st->codecpar->extradata = av_strdup("BottomUp"); | |
| 176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!st->codecpar->extradata) |
| 177 | ✗ | return AVERROR(ENOMEM); | |
| 178 | 1 | st->codecpar->extradata_size = 9; | |
| 179 | |||
| 180 | // audio - mp3 | ||
| 181 | |||
| 182 | 1 | st = avformat_new_stream(s, NULL); | |
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if(!st) |
| 184 | ✗ | return AVERROR(ENOMEM); | |
| 185 | |||
| 186 | 1 | avpriv_set_pts_info(st, 64, 1, MTV_AUDIO_SAMPLING_RATE); | |
| 187 | 1 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 188 | 1 | st->codecpar->codec_id = AV_CODEC_ID_MP3; | |
| 189 | 1 | st->codecpar->bit_rate = mtv->audio_br; | |
| 190 | 1 | ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL; | |
| 191 | |||
| 192 | // Jump over header | ||
| 193 | |||
| 194 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret64 = avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET)) < 0) |
| 195 | ✗ | return (int)ret64; | |
| 196 | |||
| 197 | 1 | return 0; | |
| 198 | |||
| 199 | } | ||
| 200 | |||
| 201 | 123 | static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 202 | { | ||
| 203 | 123 | MTVDemuxContext *mtv = s->priv_data; | |
| 204 | 123 | AVIOContext *pb = s->pb; | |
| 205 | int ret; | ||
| 206 | |||
| 207 |
2/2✓ Branch 2 taken 83 times.
✓ Branch 3 taken 40 times.
|
123 | if((avio_tell(pb) - ffformatcontext(s)->data_offset + mtv->img_segment_size) % mtv->full_segment_size) |
| 208 | { | ||
| 209 | 83 | avio_skip(pb, MTV_AUDIO_PADDING_SIZE); | |
| 210 | |||
| 211 | 83 | ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE); | |
| 212 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 80 times.
|
83 | if(ret < 0) |
| 213 | 3 | return ret; | |
| 214 | |||
| 215 | 80 | pkt->pos -= MTV_AUDIO_PADDING_SIZE; | |
| 216 | 80 | pkt->stream_index = 1; | |
| 217 | |||
| 218 | }else | ||
| 219 | { | ||
| 220 | 40 | ret = av_get_packet(pb, pkt, mtv->img_segment_size); | |
| 221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if(ret < 0) |
| 222 | ✗ | return ret; | |
| 223 | |||
| 224 | 40 | pkt->stream_index = 0; | |
| 225 | } | ||
| 226 | |||
| 227 | 120 | return ret; | |
| 228 | } | ||
| 229 | |||
| 230 | const FFInputFormat ff_mtv_demuxer = { | ||
| 231 | .p.name = "mtv", | ||
| 232 | .p.long_name = NULL_IF_CONFIG_SMALL("MTV"), | ||
| 233 | .priv_data_size = sizeof(MTVDemuxContext), | ||
| 234 | .read_probe = mtv_probe, | ||
| 235 | .read_header = mtv_read_header, | ||
| 236 | .read_packet = mtv_read_packet, | ||
| 237 | }; | ||
| 238 |