You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
6.0KB

  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. * @file libavformat/mtv.c
  23. * MTV demuxer.
  24. */
  25. #include "libavutil/bswap.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "avformat.h"
  28. #define MTV_ASUBCHUNK_DATA_SIZE 500
  29. #define MTV_HEADER_SIZE 512
  30. #define MTV_AUDIO_PADDING_SIZE 12
  31. #define AUDIO_SAMPLING_RATE 44100
  32. #define VIDEO_SID 0
  33. #define AUDIO_SID 1
  34. typedef struct MTVDemuxContext {
  35. unsigned int file_size; ///< filesize, not always right
  36. unsigned int segments; ///< number of 512 byte segments
  37. unsigned int audio_identifier; ///< 'MP3' on all files I have seen
  38. unsigned int audio_br; ///< bitrate of audio chanel (mp3)
  39. unsigned int img_colorfmt; ///< frame colorfmt rgb 565/555
  40. unsigned int img_bpp; ///< frame bits per pixel
  41. unsigned int img_width; //
  42. unsigned int img_height; //
  43. unsigned int img_segment_size; ///< size of image segment
  44. unsigned int video_fps; //
  45. unsigned int full_segment_size;
  46. } MTVDemuxContext;
  47. static int mtv_probe(AVProbeData *p)
  48. {
  49. /* Magic is 'AMV' */
  50. if(*(p->buf) != 'A' || *(p->buf+1) != 'M' || *(p->buf+2) != 'V')
  51. return 0;
  52. /* Check for nonzero in bpp and (width|height) header fields */
  53. if(!(p->buf[51] && AV_RL16(&p->buf[52]) | AV_RL16(&p->buf[54])))
  54. return 0;
  55. /* If width or height are 0 then imagesize header field should not */
  56. if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54]))
  57. {
  58. if(!!AV_RL16(&p->buf[56]))
  59. return AVPROBE_SCORE_MAX/2;
  60. else
  61. return 0;
  62. }
  63. return AVPROBE_SCORE_MAX;
  64. }
  65. static int mtv_read_header(AVFormatContext *s, AVFormatParameters *ap)
  66. {
  67. MTVDemuxContext *mtv = s->priv_data;
  68. ByteIOContext *pb = s->pb;
  69. AVStream *st;
  70. unsigned int audio_subsegments;
  71. url_fskip(pb, 3);
  72. mtv->file_size = get_le32(pb);
  73. mtv->segments = get_le32(pb);
  74. url_fskip(pb, 32);
  75. mtv->audio_identifier = get_le24(pb);
  76. mtv->audio_br = get_le16(pb);
  77. mtv->img_colorfmt = get_le24(pb);
  78. mtv->img_bpp = get_byte(pb);
  79. mtv->img_width = get_le16(pb);
  80. mtv->img_height = get_le16(pb);
  81. mtv->img_segment_size = get_le16(pb);
  82. /* Calculate width and height if missing from header */
  83. if(!mtv->img_width)
  84. mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3)
  85. / mtv->img_height;
  86. if(!mtv->img_height)
  87. mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3)
  88. / mtv->img_width;
  89. url_fskip(pb, 4);
  90. audio_subsegments = get_le16(pb);
  91. mtv->full_segment_size =
  92. audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
  93. mtv->img_segment_size;
  94. mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments;
  95. // FIXME Add sanity check here
  96. // all systems go! init decoders
  97. // video - raw rgb565
  98. st = av_new_stream(s, VIDEO_SID);
  99. if(!st)
  100. return AVERROR(ENOMEM);
  101. av_set_pts_info(st, 64, 1, mtv->video_fps);
  102. st->codec->codec_type = CODEC_TYPE_VIDEO;
  103. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  104. st->codec->codec_tag = MKTAG('R', 'G', 'B', mtv->img_bpp);
  105. st->codec->width = mtv->img_width;
  106. st->codec->height = mtv->img_height;
  107. st->codec->bits_per_coded_sample = mtv->img_bpp;
  108. st->codec->sample_rate = mtv->video_fps;
  109. st->codec->extradata = av_strdup("BottomUp");
  110. st->codec->extradata_size = 9;
  111. // audio - mp3
  112. st = av_new_stream(s, AUDIO_SID);
  113. if(!st)
  114. return AVERROR(ENOMEM);
  115. av_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
  116. st->codec->codec_type = CODEC_TYPE_AUDIO;
  117. st->codec->codec_id = CODEC_ID_MP3;
  118. st->codec->bit_rate = mtv->audio_br;
  119. st->need_parsing = AVSTREAM_PARSE_FULL;
  120. // Jump over header
  121. if(url_fseek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
  122. return AVERROR(EIO);
  123. return 0;
  124. }
  125. static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
  126. {
  127. MTVDemuxContext *mtv = s->priv_data;
  128. ByteIOContext *pb = s->pb;
  129. int ret;
  130. #if !HAVE_BIGENDIAN
  131. int i;
  132. #endif
  133. if((url_ftell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
  134. {
  135. url_fskip(pb, MTV_AUDIO_PADDING_SIZE);
  136. ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
  137. if(ret != MTV_ASUBCHUNK_DATA_SIZE)
  138. return AVERROR(EIO);
  139. pkt->pos -= MTV_AUDIO_PADDING_SIZE;
  140. pkt->stream_index = AUDIO_SID;
  141. }else
  142. {
  143. ret = av_get_packet(pb, pkt, mtv->img_segment_size);
  144. if(ret != mtv->img_segment_size)
  145. return AVERROR(EIO);
  146. #if !HAVE_BIGENDIAN
  147. /* pkt->data is GGGRRRR BBBBBGGG
  148. * and we need RRRRRGGG GGGBBBBB
  149. * for PIX_FMT_RGB565 so here we
  150. * just swap bytes as they come
  151. */
  152. for(i=0;i<mtv->img_segment_size/2;i++)
  153. *((uint16_t *)pkt->data+i) = bswap_16(*((uint16_t *)pkt->data+i));
  154. #endif
  155. pkt->stream_index = VIDEO_SID;
  156. }
  157. return ret;
  158. }
  159. AVInputFormat mtv_demuxer = {
  160. "MTV",
  161. NULL_IF_CONFIG_SMALL("MTV format"),
  162. sizeof(MTVDemuxContext),
  163. mtv_probe,
  164. mtv_read_header,
  165. mtv_read_packet,
  166. };