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.

230 lines
7.4KB

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