libavformat/mtv.c
28146a8c
 /*
  * mtv demuxer
  * Copyright (c) 2006 Reynaldo H. Verdejo Pinochet
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
ba87f080
  * @file
28146a8c
  * MTV demuxer.
  */
 
245976da
 #include "libavutil/bswap.h"
f122ef36
 #include "libavutil/intreadwrite.h"
28146a8c
 #include "avformat.h"
c3f9ebf7
 #include "internal.h"
28146a8c
 
 #define MTV_ASUBCHUNK_DATA_SIZE 500
 #define MTV_HEADER_SIZE 512
 #define MTV_AUDIO_PADDING_SIZE 12
ba15aab4
 #define MTV_IMAGE_DEFAULT_BPP 16
4d888a0a
 #define MTV_AUDIO_SAMPLING_RATE 44100
28146a8c
 
 typedef struct MTVDemuxContext {
 
7100376f
     unsigned int file_size;         ///< filesize, not always right
     unsigned int segments;          ///< number of 512 byte segments
     unsigned int audio_identifier;  ///< 'MP3' on all files I have seen
87e76ae0
     unsigned int audio_br;          ///< bitrate of audio channel (mp3)
7100376f
     unsigned int img_colorfmt;      ///< frame colorfmt rgb 565/555
     unsigned int img_bpp;           ///< frame bits per pixel
e8c26557
     unsigned int img_width;
     unsigned int img_height;
7100376f
     unsigned int img_segment_size;  ///< size of image segment
e8c26557
     unsigned int video_fps;
7100376f
     unsigned int full_segment_size;
28146a8c
 
 } MTVDemuxContext;
 
 static int mtv_probe(AVProbeData *p)
 {
30f4c58f
     /* we need at least 57 bytes from the header
      * to try parsing all required fields
      */
     if (p->buf_size < 57)
         return 0;
 
28146a8c
     /* Magic is 'AMV' */
3dc99a18
     if (*p->buf != 'A' || *(p->buf + 1) != 'M' || *(p->buf + 2) != 'V')
28146a8c
         return 0;
 
95d18099
     /* Audio magic is always MP3 */
     if (p->buf[43] != 'M' || p->buf[44] != 'P' || p->buf[45] != '3')
         return 0;
 
f122ef36
     /* Check for nonzero in bpp and (width|height) header fields */
30f4c58f
     if(!(p->buf[51] && AV_RL16(&p->buf[52]) | AV_RL16(&p->buf[54])))
f122ef36
         return 0;
 
9cf6bbb9
     /* If width or height are 0 then imagesize header field should not */
cbd8a722
     if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54]))
9cf6bbb9
     {
         if(!!AV_RL16(&p->buf[56]))
e0f8be64
             return AVPROBE_SCORE_EXTENSION;
9cf6bbb9
         else
             return 0;
     }
 
ba15aab4
     /* Image bpp is not an absolutely required
      * field as we latter claim it should be 16
      * no matter what. All samples in the wild
      * are RGB565/555.
      */
     if(p->buf[51] != MTV_IMAGE_DEFAULT_BPP)
         return AVPROBE_SCORE_EXTENSION / 2;
a8e1cbaf
 
d467e7df
     /* We had enough data to parse header values
      * but we expect to be able to get 512 bytes
      * of header to be sure.
      */
     if (p->buf_size < MTV_HEADER_SIZE)
         return AVPROBE_SCORE_EXTENSION;
 
28146a8c
     return AVPROBE_SCORE_MAX;
 }
 
6e9651d1
 static int mtv_read_header(AVFormatContext *s)
28146a8c
 {
7100376f
     MTVDemuxContext *mtv = s->priv_data;
ae628ec1
     AVIOContext   *pb  = s->pb;
7100376f
     AVStream        *st;
     unsigned int    audio_subsegments;
28146a8c
 
45a8a02a
     avio_skip(pb, 3);
b7effd4e
     mtv->file_size         = avio_rl32(pb);
     mtv->segments          = avio_rl32(pb);
45a8a02a
     avio_skip(pb, 32);
b7effd4e
     mtv->audio_identifier  = avio_rl24(pb);
     mtv->audio_br          = avio_rl16(pb);
     mtv->img_colorfmt      = avio_rl24(pb);
     mtv->img_bpp           = avio_r8(pb);
     mtv->img_width         = avio_rl16(pb);
     mtv->img_height        = avio_rl16(pb);
     mtv->img_segment_size  = avio_rl16(pb);
9cf6bbb9
 
ba15aab4
     /* Assume 16bpp even if claimed otherwise.
      * We know its going to be RGBG565/555 anyway
      */
     if (mtv->img_bpp != MTV_IMAGE_DEFAULT_BPP) {
         av_log (s, AV_LOG_WARNING, "Header claims %dbpp (!= 16). Ignoring\n",
                 mtv->img_bpp);
         mtv->img_bpp = MTV_IMAGE_DEFAULT_BPP;
     }
 
9cf6bbb9
     /* Calculate width and height if missing from header */
 
f64d7e91
     if (!mtv->img_width && mtv->img_height > 0 && mtv->img_bpp >= 8)
9cf6bbb9
         mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3)
                         / mtv->img_height;
 
f64d7e91
     if (!mtv->img_height && mtv->img_width > 0 && mtv->img_bpp >= 8)
9cf6bbb9
         mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3)
                         / mtv->img_width;
ba15aab4
 
e34ba472
     if(!mtv->img_height || !mtv->img_width || !mtv->img_segment_size){
         av_log(s, AV_LOG_ERROR, "width or height or segment_size is invalid and I cannot calculate them from other information\n");
f64d7e91
         return AVERROR_INVALIDDATA;
8b9b6332
     }
9cf6bbb9
 
45a8a02a
     avio_skip(pb, 4);
b7effd4e
     audio_subsegments = avio_rl16(pb);
feb15cee
 
     if (audio_subsegments == 0) {
1ecdf891
         avpriv_request_sample(s, "MTV files without audio");
f3298f12
         return AVERROR_PATCHWELCOME;
8b9b6332
     }
feb15cee
 
ddca6ab6
     mtv->full_segment_size =
         audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
         mtv->img_segment_size;
     mtv->video_fps         = (mtv->audio_br / 4) / audio_subsegments;
28146a8c
 
a45bdbc4
     // FIXME Add sanity check here
28146a8c
 
a45bdbc4
     // all systems go! init decoders
28146a8c
 
a45bdbc4
     // video - raw rgb565
28146a8c
 
84ad31ff
     st = avformat_new_stream(s, NULL);
28146a8c
     if(!st)
769e10f0
         return AVERROR(ENOMEM);
28146a8c
 
c3f9ebf7
     avpriv_set_pts_info(st, 64, 1, mtv->video_fps);
9200514a
     st->codecpar->codec_type      = AVMEDIA_TYPE_VIDEO;
     st->codecpar->codec_id        = AV_CODEC_ID_RAWVIDEO;
     st->codecpar->format          = AV_PIX_FMT_RGB565BE;
     st->codecpar->width           = mtv->img_width;
     st->codecpar->height          = mtv->img_height;
     st->codecpar->extradata       = av_strdup("BottomUp");
     st->codecpar->extradata_size  = 9;
28146a8c
 
a45bdbc4
     // audio - mp3
28146a8c
 
84ad31ff
     st = avformat_new_stream(s, NULL);
28146a8c
     if(!st)
769e10f0
         return AVERROR(ENOMEM);
28146a8c
 
4d888a0a
     avpriv_set_pts_info(st, 64, 1, MTV_AUDIO_SAMPLING_RATE);
9200514a
     st->codecpar->codec_type      = AVMEDIA_TYPE_AUDIO;
     st->codecpar->codec_id        = AV_CODEC_ID_MP3;
     st->codecpar->bit_rate        = mtv->audio_br;
     st->need_parsing              = AVSTREAM_PARSE_FULL;
28146a8c
 
a45bdbc4
     // Jump over header
28146a8c
 
6b4aa5da
     if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
6f3e0b21
         return AVERROR(EIO);
28146a8c
 
ccd425e7
     return 0;
28146a8c
 
 }
 
 static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     MTVDemuxContext *mtv = s->priv_data;
ae628ec1
     AVIOContext *pb = s->pb;
28146a8c
     int ret;
 
9deaec78
     if((avio_tell(pb) - s->internal->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
28146a8c
     {
45a8a02a
         avio_skip(pb, MTV_AUDIO_PADDING_SIZE);
28146a8c
 
         ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
5eef7bcd
         if(ret < 0)
             return ret;
28146a8c
 
34bfe875
         pkt->pos -= MTV_AUDIO_PADDING_SIZE;
84ad31ff
         pkt->stream_index = 1;
28146a8c
 
     }else
     {
         ret = av_get_packet(pb, pkt, mtv->img_segment_size);
5eef7bcd
         if(ret < 0)
             return ret;
28146a8c
 
84ad31ff
         pkt->stream_index = 0;
28146a8c
     }
 
ccd425e7
     return ret;
28146a8c
 }
 
c6610a21
 AVInputFormat ff_mtv_demuxer = {
68aef0b4
     .name           = "mtv",
6774247a
     .long_name      = NULL_IF_CONFIG_SMALL("MTV"),
dfc2c4d9
     .priv_data_size = sizeof(MTVDemuxContext),
     .read_probe     = mtv_probe,
     .read_header    = mtv_read_header,
     .read_packet    = mtv_read_packet,
28146a8c
 };