mtv.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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
  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 channel (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. if(p->buf[51] != 16)
  64. return AVPROBE_SCORE_MAX/4; // But we are going to assume 16bpp anyway ..
  65. return AVPROBE_SCORE_MAX;
  66. }
  67. static int mtv_read_header(AVFormatContext *s, AVFormatParameters *ap)
  68. {
  69. MTVDemuxContext *mtv = s->priv_data;
  70. AVIOContext *pb = s->pb;
  71. AVStream *st;
  72. unsigned int audio_subsegments;
  73. avio_skip(pb, 3);
  74. mtv->file_size = avio_rl32(pb);
  75. mtv->segments = avio_rl32(pb);
  76. avio_skip(pb, 32);
  77. mtv->audio_identifier = avio_rl24(pb);
  78. mtv->audio_br = avio_rl16(pb);
  79. mtv->img_colorfmt = avio_rl24(pb);
  80. mtv->img_bpp = avio_r8(pb);
  81. mtv->img_width = avio_rl16(pb);
  82. mtv->img_height = avio_rl16(pb);
  83. mtv->img_segment_size = avio_rl16(pb);
  84. /* Calculate width and height if missing from header */
  85. if(!mtv->img_width)
  86. mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3)
  87. / mtv->img_height;
  88. if(!mtv->img_height)
  89. mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3)
  90. / mtv->img_width;
  91. avio_skip(pb, 4);
  92. audio_subsegments = avio_rl16(pb);
  93. mtv->full_segment_size =
  94. audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
  95. mtv->img_segment_size;
  96. mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments;
  97. // FIXME Add sanity check here
  98. // all systems go! init decoders
  99. // video - raw rgb565
  100. st = av_new_stream(s, VIDEO_SID);
  101. if(!st)
  102. return AVERROR(ENOMEM);
  103. av_set_pts_info(st, 64, 1, mtv->video_fps);
  104. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  105. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  106. st->codec->pix_fmt = PIX_FMT_RGB565;
  107. st->codec->width = mtv->img_width;
  108. st->codec->height = mtv->img_height;
  109. st->codec->sample_rate = mtv->video_fps;
  110. st->codec->extradata = av_strdup("BottomUp");
  111. st->codec->extradata_size = 9;
  112. // audio - mp3
  113. st = av_new_stream(s, AUDIO_SID);
  114. if(!st)
  115. return AVERROR(ENOMEM);
  116. av_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
  117. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  118. st->codec->codec_id = CODEC_ID_MP3;
  119. st->codec->bit_rate = mtv->audio_br;
  120. st->need_parsing = AVSTREAM_PARSE_FULL;
  121. // Jump over header
  122. if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
  123. return AVERROR(EIO);
  124. return 0;
  125. }
  126. static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
  127. {
  128. MTVDemuxContext *mtv = s->priv_data;
  129. AVIOContext *pb = s->pb;
  130. int ret;
  131. #if !HAVE_BIGENDIAN
  132. int i;
  133. #endif
  134. if((avio_tell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
  135. {
  136. avio_skip(pb, MTV_AUDIO_PADDING_SIZE);
  137. ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
  138. if(ret < 0)
  139. return ret;
  140. pkt->pos -= MTV_AUDIO_PADDING_SIZE;
  141. pkt->stream_index = AUDIO_SID;
  142. }else
  143. {
  144. ret = av_get_packet(pb, pkt, mtv->img_segment_size);
  145. if(ret < 0)
  146. return ret;
  147. #if !HAVE_BIGENDIAN
  148. /* pkt->data is GGGRRRR BBBBBGGG
  149. * and we need RRRRRGGG GGGBBBBB
  150. * for PIX_FMT_RGB565 so here we
  151. * just swap bytes as they come
  152. */
  153. for(i=0;i<mtv->img_segment_size/2;i++)
  154. *((uint16_t *)pkt->data+i) = av_bswap16(*((uint16_t *)pkt->data+i));
  155. #endif
  156. pkt->stream_index = VIDEO_SID;
  157. }
  158. return ret;
  159. }
  160. AVInputFormat ff_mtv_demuxer = {
  161. .name = "MTV",
  162. .long_name = NULL_IF_CONFIG_SMALL("MTV format"),
  163. .priv_data_size = sizeof(MTVDemuxContext),
  164. .read_probe = mtv_probe,
  165. .read_header = mtv_read_header,
  166. .read_packet = mtv_read_packet,
  167. };