mtv.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. typedef struct MTVDemuxContext {
  33. unsigned int file_size; ///< filesize, not always right
  34. unsigned int segments; ///< number of 512 byte segments
  35. unsigned int audio_identifier; ///< 'MP3' on all files I have seen
  36. unsigned int audio_br; ///< bitrate of audio channel (mp3)
  37. unsigned int img_colorfmt; ///< frame colorfmt rgb 565/555
  38. unsigned int img_bpp; ///< frame bits per pixel
  39. unsigned int img_width; //
  40. unsigned int img_height; //
  41. unsigned int img_segment_size; ///< size of image segment
  42. unsigned int video_fps; //
  43. unsigned int full_segment_size;
  44. } MTVDemuxContext;
  45. static int mtv_probe(AVProbeData *p)
  46. {
  47. /* Magic is 'AMV' */
  48. if(*(p->buf) != 'A' || *(p->buf+1) != 'M' || *(p->buf+2) != 'V')
  49. return 0;
  50. /* Check for nonzero in bpp and (width|height) header fields */
  51. if(!(p->buf[51] && AV_RL16(&p->buf[52]) | AV_RL16(&p->buf[54])))
  52. return 0;
  53. /* If width or height are 0 then imagesize header field should not */
  54. if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54]))
  55. {
  56. if(!!AV_RL16(&p->buf[56]))
  57. return AVPROBE_SCORE_MAX/2;
  58. else
  59. return 0;
  60. }
  61. if(p->buf[51] != 16)
  62. return AVPROBE_SCORE_MAX/4; // But we are going to assume 16bpp anyway ..
  63. return AVPROBE_SCORE_MAX;
  64. }
  65. static int mtv_read_header(AVFormatContext *s, AVFormatParameters *ap)
  66. {
  67. MTVDemuxContext *mtv = s->priv_data;
  68. AVIOContext *pb = s->pb;
  69. AVStream *st;
  70. unsigned int audio_subsegments;
  71. avio_skip(pb, 3);
  72. mtv->file_size = avio_rl32(pb);
  73. mtv->segments = avio_rl32(pb);
  74. avio_skip(pb, 32);
  75. mtv->audio_identifier = avio_rl24(pb);
  76. mtv->audio_br = avio_rl16(pb);
  77. mtv->img_colorfmt = avio_rl24(pb);
  78. mtv->img_bpp = avio_r8(pb);
  79. mtv->img_width = avio_rl16(pb);
  80. mtv->img_height = avio_rl16(pb);
  81. mtv->img_segment_size = avio_rl16(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. avio_skip(pb, 4);
  90. audio_subsegments = avio_rl16(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 = avformat_new_stream(s, NULL);
  99. if(!st)
  100. return AVERROR(ENOMEM);
  101. av_set_pts_info(st, 64, 1, mtv->video_fps);
  102. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  103. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  104. st->codec->pix_fmt = PIX_FMT_RGB565;
  105. st->codec->width = mtv->img_width;
  106. st->codec->height = mtv->img_height;
  107. st->codec->sample_rate = mtv->video_fps;
  108. st->codec->extradata = av_strdup("BottomUp");
  109. st->codec->extradata_size = 9;
  110. // audio - mp3
  111. st = avformat_new_stream(s, NULL);
  112. if(!st)
  113. return AVERROR(ENOMEM);
  114. av_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
  115. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  116. st->codec->codec_id = CODEC_ID_MP3;
  117. st->codec->bit_rate = mtv->audio_br;
  118. st->need_parsing = AVSTREAM_PARSE_FULL;
  119. // Jump over header
  120. if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
  121. return AVERROR(EIO);
  122. return 0;
  123. }
  124. static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
  125. {
  126. MTVDemuxContext *mtv = s->priv_data;
  127. AVIOContext *pb = s->pb;
  128. int ret;
  129. #if !HAVE_BIGENDIAN
  130. int i;
  131. #endif
  132. if((avio_tell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
  133. {
  134. avio_skip(pb, MTV_AUDIO_PADDING_SIZE);
  135. ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
  136. if(ret < 0)
  137. return ret;
  138. pkt->pos -= MTV_AUDIO_PADDING_SIZE;
  139. pkt->stream_index = 1;
  140. }else
  141. {
  142. ret = av_get_packet(pb, pkt, mtv->img_segment_size);
  143. if(ret < 0)
  144. return ret;
  145. #if !HAVE_BIGENDIAN
  146. /* pkt->data is GGGRRRR BBBBBGGG
  147. * and we need RRRRRGGG GGGBBBBB
  148. * for PIX_FMT_RGB565 so here we
  149. * just swap bytes as they come
  150. */
  151. for(i=0;i<mtv->img_segment_size/2;i++)
  152. *((uint16_t *)pkt->data+i) = av_bswap16(*((uint16_t *)pkt->data+i));
  153. #endif
  154. pkt->stream_index = 0;
  155. }
  156. return ret;
  157. }
  158. AVInputFormat ff_mtv_demuxer = {
  159. .name = "MTV",
  160. .long_name = NULL_IF_CONFIG_SMALL("MTV format"),
  161. .priv_data_size = sizeof(MTVDemuxContext),
  162. .read_probe = mtv_probe,
  163. .read_header = mtv_read_header,
  164. .read_packet = mtv_read_packet,
  165. };