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.

227 lines
7.3KB

  1. /*
  2. * id RoQ (.roq) File Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  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. * id RoQ format file demuxer
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * for more information on the .roq file format, visit:
  26. * http://www.csse.monash.edu.au/~timf/
  27. */
  28. #include "libavutil/intreadwrite.h"
  29. #include "avformat.h"
  30. #include "internal.h"
  31. #define RoQ_MAGIC_NUMBER 0x1084
  32. #define RoQ_CHUNK_PREAMBLE_SIZE 8
  33. #define RoQ_AUDIO_SAMPLE_RATE 22050
  34. #define RoQ_CHUNKS_TO_SCAN 30
  35. #define RoQ_INFO 0x1001
  36. #define RoQ_QUAD_CODEBOOK 0x1002
  37. #define RoQ_QUAD_VQ 0x1011
  38. #define RoQ_SOUND_MONO 0x1020
  39. #define RoQ_SOUND_STEREO 0x1021
  40. typedef struct RoqDemuxContext {
  41. int width;
  42. int height;
  43. int audio_channels;
  44. int video_stream_index;
  45. int audio_stream_index;
  46. int64_t video_pts;
  47. unsigned int audio_frame_count;
  48. } RoqDemuxContext;
  49. static int roq_probe(AVProbeData *p)
  50. {
  51. if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
  52. (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
  53. return 0;
  54. return AVPROBE_SCORE_MAX;
  55. }
  56. static int roq_read_header(AVFormatContext *s,
  57. AVFormatParameters *ap)
  58. {
  59. RoqDemuxContext *roq = s->priv_data;
  60. AVIOContext *pb = s->pb;
  61. int framerate;
  62. AVStream *st;
  63. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  64. /* get the main header */
  65. if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  66. RoQ_CHUNK_PREAMBLE_SIZE)
  67. return AVERROR(EIO);
  68. framerate = AV_RL16(&preamble[6]);
  69. /* init private context parameters */
  70. roq->width = roq->height = roq->audio_channels = roq->video_pts =
  71. roq->audio_frame_count = 0;
  72. roq->audio_stream_index = -1;
  73. st = avformat_new_stream(s, NULL);
  74. if (!st)
  75. return AVERROR(ENOMEM);
  76. avpriv_set_pts_info(st, 63, 1, framerate);
  77. roq->video_stream_index = st->index;
  78. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  79. st->codec->codec_id = CODEC_ID_ROQ;
  80. st->codec->codec_tag = 0; /* no fourcc */
  81. return 0;
  82. }
  83. static int roq_read_packet(AVFormatContext *s,
  84. AVPacket *pkt)
  85. {
  86. RoqDemuxContext *roq = s->priv_data;
  87. AVIOContext *pb = s->pb;
  88. int ret = 0;
  89. unsigned int chunk_size;
  90. unsigned int chunk_type;
  91. unsigned int codebook_size;
  92. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  93. int packet_read = 0;
  94. int64_t codebook_offset;
  95. while (!packet_read) {
  96. if (url_feof(s->pb))
  97. return AVERROR(EIO);
  98. /* get the next chunk preamble */
  99. if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
  100. RoQ_CHUNK_PREAMBLE_SIZE)
  101. return AVERROR(EIO);
  102. chunk_type = AV_RL16(&preamble[0]);
  103. chunk_size = AV_RL32(&preamble[2]);
  104. if(chunk_size > INT_MAX)
  105. return AVERROR_INVALIDDATA;
  106. switch (chunk_type) {
  107. case RoQ_INFO:
  108. if (!roq->width || !roq->height) {
  109. AVStream *st = s->streams[roq->video_stream_index];
  110. if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE)
  111. return AVERROR(EIO);
  112. st->codec->width = roq->width = AV_RL16(preamble);
  113. st->codec->height = roq->height = AV_RL16(preamble + 2);
  114. break;
  115. }
  116. /* don't care about this chunk anymore */
  117. avio_skip(pb, RoQ_CHUNK_PREAMBLE_SIZE);
  118. break;
  119. case RoQ_QUAD_CODEBOOK:
  120. /* packet needs to contain both this codebook and next VQ chunk */
  121. codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
  122. codebook_size = chunk_size;
  123. avio_skip(pb, codebook_size);
  124. if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  125. RoQ_CHUNK_PREAMBLE_SIZE)
  126. return AVERROR(EIO);
  127. chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
  128. codebook_size;
  129. /* rewind */
  130. avio_seek(pb, codebook_offset, SEEK_SET);
  131. /* load up the packet */
  132. ret= av_get_packet(pb, pkt, chunk_size);
  133. if (ret != chunk_size)
  134. return AVERROR(EIO);
  135. pkt->stream_index = roq->video_stream_index;
  136. pkt->pts = roq->video_pts++;
  137. packet_read = 1;
  138. break;
  139. case RoQ_SOUND_MONO:
  140. case RoQ_SOUND_STEREO:
  141. if (roq->audio_stream_index == -1) {
  142. AVStream *st = avformat_new_stream(s, NULL);
  143. if (!st)
  144. return AVERROR(ENOMEM);
  145. avpriv_set_pts_info(st, 32, 1, RoQ_AUDIO_SAMPLE_RATE);
  146. roq->audio_stream_index = st->index;
  147. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  148. st->codec->codec_id = CODEC_ID_ROQ_DPCM;
  149. st->codec->codec_tag = 0; /* no tag */
  150. st->codec->channels = roq->audio_channels = chunk_type == RoQ_SOUND_STEREO ? 2 : 1;
  151. st->codec->sample_rate = RoQ_AUDIO_SAMPLE_RATE;
  152. st->codec->bits_per_coded_sample = 16;
  153. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  154. st->codec->bits_per_coded_sample;
  155. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  156. }
  157. case RoQ_QUAD_VQ:
  158. /* load up the packet */
  159. if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
  160. return AVERROR(EIO);
  161. /* copy over preamble */
  162. memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
  163. if (chunk_type == RoQ_QUAD_VQ) {
  164. pkt->stream_index = roq->video_stream_index;
  165. pkt->pts = roq->video_pts++;
  166. } else {
  167. pkt->stream_index = roq->audio_stream_index;
  168. pkt->pts = roq->audio_frame_count;
  169. roq->audio_frame_count += (chunk_size / roq->audio_channels);
  170. }
  171. pkt->pos= avio_tell(pb);
  172. ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
  173. chunk_size);
  174. if (ret != chunk_size)
  175. ret = AVERROR(EIO);
  176. packet_read = 1;
  177. break;
  178. default:
  179. av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
  180. return AVERROR_INVALIDDATA;
  181. }
  182. }
  183. return ret;
  184. }
  185. AVInputFormat ff_roq_demuxer = {
  186. .name = "RoQ",
  187. .long_name = NULL_IF_CONFIG_SMALL("id RoQ format"),
  188. .priv_data_size = sizeof(RoqDemuxContext),
  189. .read_probe = roq_probe,
  190. .read_header = roq_read_header,
  191. .read_packet = roq_read_packet,
  192. };