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.

185 lines
5.2KB

  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 mtv.c
  23. * MTV demuxer.
  24. */
  25. #include "avformat.h"
  26. #include "bswap.h"
  27. #define MTV_ASUBCHUNK_DATA_SIZE 500
  28. #define MTV_HEADER_SIZE 512
  29. #define MTV_AUDIO_PADDING_SIZE 12
  30. #define AUDIO_SAMPLING_RATE 44100
  31. #define VIDEO_SID 0
  32. #define AUDIO_SID 1
  33. typedef struct MTVDemuxContext {
  34. unsigned int file_size; ///< filesize, not always right
  35. unsigned int segments; ///< number of 512 byte segments
  36. unsigned int audio_identifier; ///< 'MP3' on all files I have seen
  37. unsigned int audio_br; ///< bitrate of audio chanel (mp3)
  38. unsigned int img_colorfmt; ///< frame colorfmt rgb 565/555
  39. unsigned int img_bpp; ///< frame bits per pixel
  40. unsigned int img_width; //
  41. unsigned int img_height; //
  42. unsigned int img_segment_size; ///< size of image segment
  43. unsigned int video_fps; //
  44. unsigned int audio_subsegments; ///< audio subsegments on one segment
  45. uint8_t audio_packet_count;
  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. return AVPROBE_SCORE_MAX;
  53. }
  54. static int mtv_read_header(AVFormatContext *s, AVFormatParameters *ap)
  55. {
  56. MTVDemuxContext *mtv = s->priv_data;
  57. ByteIOContext *pb = &s->pb;
  58. AVStream *st;
  59. url_fskip(pb, 3);
  60. mtv->file_size = get_le32(pb);
  61. mtv->segments = get_le32(pb);
  62. url_fskip(pb, 32);
  63. mtv->audio_identifier = get_le24(pb);
  64. mtv->audio_br = get_le16(pb);
  65. mtv->img_colorfmt = get_le24(pb);
  66. mtv->img_bpp = get_byte(pb);
  67. mtv->img_width = get_le16(pb);
  68. mtv->img_height = get_le16(pb);
  69. mtv->img_segment_size = get_le16(pb);
  70. url_fskip(pb, 4);
  71. mtv->audio_subsegments = get_le16(pb);
  72. mtv->video_fps = (mtv->audio_br / 4) / mtv->audio_subsegments;
  73. /* FIXME Add sanity check here */
  74. /* first packet is always audio*/
  75. mtv->audio_packet_count = 1;
  76. /* all systems go! init decoders */
  77. /* video - raw rgb565 */
  78. st = av_new_stream(s, VIDEO_SID);
  79. if(!st)
  80. return AVERROR(ENOMEM);
  81. av_set_pts_info(st, 64, 1, mtv->video_fps);
  82. st->codec->codec_type = CODEC_TYPE_VIDEO;
  83. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  84. st->codec->codec_tag = MKTAG('R', 'G', 'B', mtv->img_bpp);
  85. st->codec->width = mtv->img_width;
  86. st->codec->height = mtv->img_height;
  87. st->codec->bits_per_sample = mtv->img_bpp;
  88. st->codec->sample_rate = mtv->video_fps;
  89. /* audio - mp3 */
  90. st = av_new_stream(s, AUDIO_SID);
  91. if(!st)
  92. return AVERROR(ENOMEM);
  93. av_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
  94. st->codec->codec_type = CODEC_TYPE_AUDIO;
  95. st->codec->codec_id = CODEC_ID_MP3;
  96. st->codec->bit_rate = mtv->audio_br;
  97. st->need_parsing = AVSTREAM_PARSE_FULL;
  98. /* Jump over header */
  99. if(url_fseek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
  100. return AVERROR(EIO);
  101. return(0);
  102. }
  103. static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
  104. {
  105. MTVDemuxContext *mtv = s->priv_data;
  106. ByteIOContext *pb = &s->pb;
  107. int ret;
  108. #ifndef WORDS_BIGENDIAN
  109. int i;
  110. #endif
  111. ret = 0;
  112. if(mtv->audio_subsegments >= mtv->audio_packet_count)
  113. {
  114. url_fskip(pb, MTV_AUDIO_PADDING_SIZE);
  115. ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
  116. if(ret != MTV_ASUBCHUNK_DATA_SIZE)
  117. return AVERROR(EIO);
  118. mtv->audio_packet_count++;
  119. pkt->stream_index = AUDIO_SID;
  120. }else
  121. {
  122. ret = av_get_packet(pb, pkt, mtv->img_segment_size);
  123. if(ret != mtv->img_segment_size)
  124. return AVERROR(EIO);
  125. #ifndef WORDS_BIGENDIAN
  126. /* pkt->data is GGGRRRR BBBBBGGG
  127. * and we need RRRRRGGG GGGBBBBB
  128. * for PIX_FMT_RGB565 so here we
  129. * just swap bytes as they come
  130. */
  131. for(i=0;i<mtv->img_segment_size/2;i++)
  132. *((uint16_t *)pkt->data+i) = bswap_16(*((uint16_t *)pkt->data+i));
  133. #endif
  134. mtv->audio_packet_count = 1;
  135. pkt->stream_index = VIDEO_SID;
  136. }
  137. return(ret);
  138. }
  139. AVInputFormat mtv_demuxer = {
  140. "MTV",
  141. "MTV format",
  142. sizeof(MTVDemuxContext),
  143. mtv_probe,
  144. mtv_read_header,
  145. mtv_read_packet,
  146. };