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.4KB

  1. /*
  2. * id RoQ (.roq) File Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 frame_rate;
  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. {
  59. RoqDemuxContext *roq = s->priv_data;
  60. AVIOContext *pb = s->pb;
  61. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  62. /* get the main header */
  63. if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  64. RoQ_CHUNK_PREAMBLE_SIZE)
  65. return AVERROR(EIO);
  66. roq->frame_rate = AV_RL16(&preamble[6]);
  67. /* init private context parameters */
  68. roq->width = roq->height = roq->audio_channels = roq->video_pts =
  69. roq->audio_frame_count = 0;
  70. roq->audio_stream_index = -1;
  71. roq->video_stream_index = -1;
  72. s->ctx_flags |= AVFMTCTX_NOHEADER;
  73. return 0;
  74. }
  75. static int roq_read_packet(AVFormatContext *s,
  76. AVPacket *pkt)
  77. {
  78. RoqDemuxContext *roq = s->priv_data;
  79. AVIOContext *pb = s->pb;
  80. int ret = 0;
  81. unsigned int chunk_size;
  82. unsigned int chunk_type;
  83. unsigned int codebook_size;
  84. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  85. int packet_read = 0;
  86. int64_t codebook_offset;
  87. while (!packet_read) {
  88. if (s->pb->eof_reached)
  89. return AVERROR(EIO);
  90. /* get the next chunk preamble */
  91. if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
  92. RoQ_CHUNK_PREAMBLE_SIZE)
  93. return AVERROR(EIO);
  94. chunk_type = AV_RL16(&preamble[0]);
  95. chunk_size = AV_RL32(&preamble[2]);
  96. if(chunk_size > INT_MAX)
  97. return AVERROR_INVALIDDATA;
  98. switch (chunk_type) {
  99. case RoQ_INFO:
  100. if (roq->video_stream_index == -1) {
  101. AVStream *st = avformat_new_stream(s, NULL);
  102. if (!st)
  103. return AVERROR(ENOMEM);
  104. avpriv_set_pts_info(st, 63, 1, roq->frame_rate);
  105. roq->video_stream_index = st->index;
  106. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  107. st->codec->codec_id = CODEC_ID_ROQ;
  108. st->codec->codec_tag = 0; /* no fourcc */
  109. if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE)
  110. return AVERROR(EIO);
  111. st->codec->width = roq->width = AV_RL16(preamble);
  112. st->codec->height = roq->height = AV_RL16(preamble + 2);
  113. break;
  114. }
  115. /* don't care about this chunk anymore */
  116. avio_skip(pb, RoQ_CHUNK_PREAMBLE_SIZE);
  117. break;
  118. case RoQ_QUAD_CODEBOOK:
  119. /* packet needs to contain both this codebook and next VQ chunk */
  120. codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
  121. codebook_size = chunk_size;
  122. avio_skip(pb, codebook_size);
  123. if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  124. RoQ_CHUNK_PREAMBLE_SIZE)
  125. return AVERROR(EIO);
  126. chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
  127. codebook_size;
  128. /* rewind */
  129. avio_seek(pb, codebook_offset, SEEK_SET);
  130. /* load up the packet */
  131. ret= av_get_packet(pb, pkt, chunk_size);
  132. if (ret != chunk_size)
  133. return AVERROR(EIO);
  134. pkt->stream_index = roq->video_stream_index;
  135. pkt->pts = roq->video_pts++;
  136. packet_read = 1;
  137. break;
  138. case RoQ_SOUND_MONO:
  139. case RoQ_SOUND_STEREO:
  140. if (roq->audio_stream_index == -1) {
  141. AVStream *st = avformat_new_stream(s, NULL);
  142. if (!st)
  143. return AVERROR(ENOMEM);
  144. avpriv_set_pts_info(st, 32, 1, RoQ_AUDIO_SAMPLE_RATE);
  145. roq->audio_stream_index = st->index;
  146. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  147. st->codec->codec_id = CODEC_ID_ROQ_DPCM;
  148. st->codec->codec_tag = 0; /* no tag */
  149. st->codec->channels = roq->audio_channels = chunk_type == RoQ_SOUND_STEREO ? 2 : 1;
  150. st->codec->sample_rate = RoQ_AUDIO_SAMPLE_RATE;
  151. st->codec->bits_per_coded_sample = 16;
  152. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  153. st->codec->bits_per_coded_sample;
  154. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  155. }
  156. case RoQ_QUAD_VQ:
  157. /* load up the packet */
  158. if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
  159. return AVERROR(EIO);
  160. /* copy over preamble */
  161. memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
  162. if (chunk_type == RoQ_QUAD_VQ) {
  163. pkt->stream_index = roq->video_stream_index;
  164. pkt->pts = roq->video_pts++;
  165. } else {
  166. pkt->stream_index = roq->audio_stream_index;
  167. pkt->pts = roq->audio_frame_count;
  168. roq->audio_frame_count += (chunk_size / roq->audio_channels);
  169. }
  170. pkt->pos= avio_tell(pb);
  171. ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
  172. chunk_size);
  173. if (ret != chunk_size)
  174. ret = AVERROR(EIO);
  175. packet_read = 1;
  176. break;
  177. default:
  178. av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
  179. return AVERROR_INVALIDDATA;
  180. }
  181. }
  182. return ret;
  183. }
  184. AVInputFormat ff_roq_demuxer = {
  185. .name = "roq",
  186. .long_name = NULL_IF_CONFIG_SMALL("id RoQ format"),
  187. .priv_data_size = sizeof(RoqDemuxContext),
  188. .read_probe = roq_probe,
  189. .read_header = roq_read_header,
  190. .read_packet = roq_read_packet,
  191. };