FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mtv.c
Go to the documentation of this file.
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/bswap.h"
28 #include "libavutil/intreadwrite.h"
29 #include "avformat.h"
30 #include "internal.h"
31 
32 #define MTV_ASUBCHUNK_DATA_SIZE 500
33 #define MTV_HEADER_SIZE 512
34 #define MTV_AUDIO_PADDING_SIZE 12
35 #define MTV_IMAGE_DEFAULT_BPP 16
36 #define MTV_AUDIO_SAMPLING_RATE 44100
37 
38 typedef struct MTVDemuxContext {
39 
40  unsigned int file_size; ///< filesize, not always right
41  unsigned int segments; ///< number of 512 byte segments
42  unsigned int audio_identifier; ///< 'MP3' on all files I have seen
43  unsigned int audio_br; ///< bitrate of audio channel (mp3)
44  unsigned int img_colorfmt; ///< frame colorfmt rgb 565/555
45  unsigned int img_bpp; ///< frame bits per pixel
46  unsigned int img_width;
47  unsigned int img_height;
48  unsigned int img_segment_size; ///< size of image segment
49  unsigned int video_fps;
50  unsigned int full_segment_size;
51 
53 
54 static int mtv_probe(AVProbeData *p)
55 {
56  /* we need at least 57 bytes from the header
57  * to try parsing all required fields
58  */
59  if (p->buf_size < 57)
60  return 0;
61 
62  /* Magic is 'AMV' */
63  if (*p->buf != 'A' || *(p->buf + 1) != 'M' || *(p->buf + 2) != 'V')
64  return 0;
65 
66  /* Audio magic is always MP3 */
67  if (p->buf[43] != 'M' || p->buf[44] != 'P' || p->buf[45] != '3')
68  return 0;
69 
70  /* Check for nonzero in bpp and (width|height) header fields */
71  if(!(p->buf[51] && AV_RL16(&p->buf[52]) | AV_RL16(&p->buf[54])))
72  return 0;
73 
74  /* If width or height are 0 then imagesize header field should not */
75  if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54]))
76  {
77  if(!!AV_RL16(&p->buf[56]))
79  else
80  return 0;
81  }
82 
83  /* Image bpp is not an absolutely required
84  * field as we latter claim it should be 16
85  * no matter what. All samples in the wild
86  * are RGB565/555.
87  */
88  if(p->buf[51] != MTV_IMAGE_DEFAULT_BPP)
89  return AVPROBE_SCORE_EXTENSION / 2;
90 
91  /* We had enough data to parse header values
92  * but we expect to be able to get 512 bytes
93  * of header to be sure.
94  */
95  if (p->buf_size < MTV_HEADER_SIZE)
97 
98  return AVPROBE_SCORE_MAX;
99 }
100 
102 {
103  MTVDemuxContext *mtv = s->priv_data;
104  AVIOContext *pb = s->pb;
105  AVStream *st;
106  unsigned int audio_subsegments;
107 
108  avio_skip(pb, 3);
109  mtv->file_size = avio_rl32(pb);
110  mtv->segments = avio_rl32(pb);
111  avio_skip(pb, 32);
112  mtv->audio_identifier = avio_rl24(pb);
113  mtv->audio_br = avio_rl16(pb);
114  mtv->img_colorfmt = avio_rl24(pb);
115  mtv->img_bpp = avio_r8(pb);
116  mtv->img_width = avio_rl16(pb);
117  mtv->img_height = avio_rl16(pb);
118  mtv->img_segment_size = avio_rl16(pb);
119 
120  /* Assume 16bpp even if claimed otherwise.
121  * We know its going to be RGBG565/555 anyway
122  */
123  if (mtv->img_bpp != MTV_IMAGE_DEFAULT_BPP) {
124  av_log (s, AV_LOG_WARNING, "Header claims %dbpp (!= 16). Ignoring\n",
125  mtv->img_bpp);
127  }
128 
129  /* Calculate width and height if missing from header */
130 
131  if (!mtv->img_width && mtv->img_height > 0 && mtv->img_bpp >= 8)
132  mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3)
133  / mtv->img_height;
134 
135  if (!mtv->img_height && mtv->img_width > 0 && mtv->img_bpp >= 8)
136  mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3)
137  / mtv->img_width;
138 
139  if(!mtv->img_height || !mtv->img_width || !mtv->img_segment_size){
140  av_log(s, AV_LOG_ERROR, "width or height or segment_size is invalid and I cannot calculate them from other information\n");
141  return AVERROR_INVALIDDATA;
142  }
143 
144  avio_skip(pb, 4);
145  audio_subsegments = avio_rl16(pb);
146 
147  if (audio_subsegments == 0) {
148  avpriv_request_sample(s, "MTV files without audio");
149  return AVERROR_PATCHWELCOME;
150  }
151 
152  mtv->full_segment_size =
153  audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
154  mtv->img_segment_size;
155  mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments;
156 
157  // FIXME Add sanity check here
158 
159  // all systems go! init decoders
160 
161  // video - raw rgb565
162 
163  st = avformat_new_stream(s, NULL);
164  if(!st)
165  return AVERROR(ENOMEM);
166 
167  avpriv_set_pts_info(st, 64, 1, mtv->video_fps);
171  st->codec->width = mtv->img_width;
172  st->codec->height = mtv->img_height;
173  st->codec->extradata = av_strdup("BottomUp");
174  st->codec->extradata_size = 9;
175 
176  // audio - mp3
177 
178  st = avformat_new_stream(s, NULL);
179  if(!st)
180  return AVERROR(ENOMEM);
181 
185  st->codec->bit_rate = mtv->audio_br;
187 
188  // Jump over header
189 
190  if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
191  return AVERROR(EIO);
192 
193  return 0;
194 
195 }
196 
198 {
199  MTVDemuxContext *mtv = s->priv_data;
200  AVIOContext *pb = s->pb;
201  int ret;
202 
203  if((avio_tell(pb) - s->internal->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
204  {
206 
207  ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
208  if(ret < 0)
209  return ret;
210 
211  pkt->pos -= MTV_AUDIO_PADDING_SIZE;
212  pkt->stream_index = 1;
213 
214  }else
215  {
216  ret = av_get_packet(pb, pkt, mtv->img_segment_size);
217  if(ret < 0)
218  return ret;
219 
220  pkt->stream_index = 0;
221  }
222 
223  return ret;
224 }
225 
227  .name = "mtv",
228  .long_name = NULL_IF_CONFIG_SMALL("MTV"),
229  .priv_data_size = sizeof(MTVDemuxContext),
233 };
#define MTV_ASUBCHUNK_DATA_SIZE
Definition: mtv.c:32
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define MTV_AUDIO_SAMPLING_RATE
Definition: mtv.c:36
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1448
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:4083
unsigned int img_bpp
frame bits per pixel
Definition: mtv.c:45
int64_t data_offset
offset of the first packet
Definition: internal.h:80
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:204
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1698
unsigned int img_width
Definition: mtv.c:46
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1722
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:277
static AVPacket pkt
unsigned int segments
number of 512 byte segments
Definition: mtv.c:41
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
unsigned int video_fps
Definition: mtv.c:49
#define MTV_HEADER_SIZE
Definition: mtv.c:33
Format I/O context.
Definition: avformat.h:1273
unsigned int img_colorfmt
frame colorfmt rgb 565/555
Definition: mtv.c:44
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
AVInputFormat ff_mtv_demuxer
Definition: mtv.c:226
enum AVStreamParseType need_parsing
Definition: avformat.h:1034
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1617
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3749
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:114
static int mtv_read_header(AVFormatContext *s)
Definition: mtv.c:101
unsigned int audio_identifier
'MP3' on all files I have seen
Definition: mtv.c:42
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:244
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:390
#define av_log(a,...)
unsigned int file_size
filesize, not always right
Definition: mtv.c:40
#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:659
#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:175
unsigned int full_segment_size
Definition: mtv.c:50
#define MTV_IMAGE_DEFAULT_BPP
Definition: mtv.c:35
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:425
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:529
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:451
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
int bit_rate
the average bitrate
Definition: avcodec.h:1567
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1681
#define MTV_AUDIO_PADDING_SIZE
Definition: mtv.c:34
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:623
Stream structure.
Definition: avformat.h:842
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
enum AVMediaType codec_type
Definition: avcodec.h:1510
enum AVCodecID codec_id
Definition: avcodec.h:1519
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
unsigned int img_segment_size
size of image segment
Definition: mtv.c:48
int extradata_size
Definition: avcodec.h:1618
byte swapping routines
unsigned int img_height
Definition: mtv.c:47
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:458
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
static int mtv_probe(AVProbeData *p)
Definition: mtv.c:54
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
full parsing and repack
Definition: avformat.h:774
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:643
Main libavformat public API header.
if(ret< 0)
Definition: vf_mcdeint.c:280
unsigned int audio_br
bitrate of audio channel (mp3)
Definition: mtv.c:43
void * priv_data
Format private data.
Definition: avformat.h:1301
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:628
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:651
static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mtv.c:197
int stream_index
Definition: avcodec.h:1425
This structure stores compressed data.
Definition: avcodec.h:1400