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.

289 lines
8.7KB

  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 idroq.c
  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 "avformat.h"
  29. #define RoQ_MAGIC_NUMBER 0x1084
  30. #define RoQ_CHUNK_PREAMBLE_SIZE 8
  31. #define RoQ_AUDIO_SAMPLE_RATE 22050
  32. #define RoQ_CHUNKS_TO_SCAN 30
  33. #define RoQ_INFO 0x1001
  34. #define RoQ_QUAD_CODEBOOK 0x1002
  35. #define RoQ_QUAD_VQ 0x1011
  36. #define RoQ_SOUND_MONO 0x1020
  37. #define RoQ_SOUND_STEREO 0x1021
  38. typedef struct RoqDemuxContext {
  39. int width;
  40. int height;
  41. int audio_channels;
  42. int framerate;
  43. int frame_pts_inc;
  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. ByteIOContext *pb = &s->pb;
  61. AVStream *st;
  62. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  63. int i;
  64. unsigned int chunk_size;
  65. unsigned int chunk_type;
  66. /* get the main header */
  67. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  68. RoQ_CHUNK_PREAMBLE_SIZE)
  69. return AVERROR(EIO);
  70. roq->framerate = AV_RL16(&preamble[6]);
  71. roq->frame_pts_inc = 90000 / roq->framerate;
  72. /* init private context parameters */
  73. roq->width = roq->height = roq->audio_channels = roq->video_pts =
  74. roq->audio_frame_count = 0;
  75. /* scan the first n chunks searching for A/V parameters */
  76. for (i = 0; i < RoQ_CHUNKS_TO_SCAN; i++) {
  77. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  78. RoQ_CHUNK_PREAMBLE_SIZE)
  79. return AVERROR(EIO);
  80. chunk_type = AV_RL16(&preamble[0]);
  81. chunk_size = AV_RL32(&preamble[2]);
  82. switch (chunk_type) {
  83. case RoQ_INFO:
  84. /* fetch the width and height; reuse the preamble bytes */
  85. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  86. RoQ_CHUNK_PREAMBLE_SIZE)
  87. return AVERROR(EIO);
  88. roq->width = AV_RL16(&preamble[0]);
  89. roq->height = AV_RL16(&preamble[2]);
  90. break;
  91. case RoQ_QUAD_CODEBOOK:
  92. case RoQ_QUAD_VQ:
  93. /* ignore during this scan */
  94. url_fseek(pb, chunk_size, SEEK_CUR);
  95. break;
  96. case RoQ_SOUND_MONO:
  97. roq->audio_channels = 1;
  98. url_fseek(pb, chunk_size, SEEK_CUR);
  99. break;
  100. case RoQ_SOUND_STEREO:
  101. roq->audio_channels = 2;
  102. url_fseek(pb, chunk_size, SEEK_CUR);
  103. break;
  104. default:
  105. av_log(s, AV_LOG_ERROR, " unknown RoQ chunk type (%04X)\n", AV_RL16(&preamble[0]));
  106. return AVERROR_INVALIDDATA;
  107. break;
  108. }
  109. /* if all necessary parameters have been gathered, exit early */
  110. if ((roq->width && roq->height) && roq->audio_channels)
  111. break;
  112. }
  113. /* seek back to the first chunk */
  114. url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_SET);
  115. /* initialize the decoders */
  116. st = av_new_stream(s, 0);
  117. if (!st)
  118. return AVERROR(ENOMEM);
  119. /* set the pts reference (1 pts = 1/90000) */
  120. av_set_pts_info(st, 33, 1, 90000);
  121. roq->video_stream_index = st->index;
  122. st->codec->codec_type = CODEC_TYPE_VIDEO;
  123. st->codec->codec_id = CODEC_ID_ROQ;
  124. st->codec->codec_tag = 0; /* no fourcc */
  125. st->codec->width = roq->width;
  126. st->codec->height = roq->height;
  127. if (roq->audio_channels) {
  128. st = av_new_stream(s, 0);
  129. if (!st)
  130. return AVERROR(ENOMEM);
  131. av_set_pts_info(st, 33, 1, 90000);
  132. roq->audio_stream_index = st->index;
  133. st->codec->codec_type = CODEC_TYPE_AUDIO;
  134. st->codec->codec_id = CODEC_ID_ROQ_DPCM;
  135. st->codec->codec_tag = 0; /* no tag */
  136. st->codec->channels = roq->audio_channels;
  137. st->codec->sample_rate = RoQ_AUDIO_SAMPLE_RATE;
  138. st->codec->bits_per_sample = 16;
  139. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  140. st->codec->bits_per_sample;
  141. st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
  142. }
  143. return 0;
  144. }
  145. static int roq_read_packet(AVFormatContext *s,
  146. AVPacket *pkt)
  147. {
  148. RoqDemuxContext *roq = s->priv_data;
  149. ByteIOContext *pb = &s->pb;
  150. int ret = 0;
  151. unsigned int chunk_size;
  152. unsigned int chunk_type;
  153. unsigned int codebook_size;
  154. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  155. int packet_read = 0;
  156. offset_t codebook_offset;
  157. while (!packet_read) {
  158. if (url_feof(&s->pb))
  159. return AVERROR(EIO);
  160. /* get the next chunk preamble */
  161. if ((ret = get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
  162. RoQ_CHUNK_PREAMBLE_SIZE)
  163. return AVERROR(EIO);
  164. chunk_type = AV_RL16(&preamble[0]);
  165. chunk_size = AV_RL32(&preamble[2]);
  166. if(chunk_size > INT_MAX)
  167. return AVERROR_INVALIDDATA;
  168. switch (chunk_type) {
  169. case RoQ_INFO:
  170. /* don't care about this chunk anymore */
  171. url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_CUR);
  172. break;
  173. case RoQ_QUAD_CODEBOOK:
  174. /* packet needs to contain both this codebook and next VQ chunk */
  175. codebook_offset = url_ftell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
  176. codebook_size = chunk_size;
  177. url_fseek(pb, codebook_size, SEEK_CUR);
  178. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  179. RoQ_CHUNK_PREAMBLE_SIZE)
  180. return AVERROR(EIO);
  181. chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
  182. codebook_size;
  183. /* rewind */
  184. url_fseek(pb, codebook_offset, SEEK_SET);
  185. /* load up the packet */
  186. ret= av_get_packet(pb, pkt, chunk_size);
  187. if (ret != chunk_size)
  188. return AVERROR(EIO);
  189. pkt->stream_index = roq->video_stream_index;
  190. pkt->pts = roq->video_pts;
  191. roq->video_pts += roq->frame_pts_inc;
  192. packet_read = 1;
  193. break;
  194. case RoQ_SOUND_MONO:
  195. case RoQ_SOUND_STEREO:
  196. case RoQ_QUAD_VQ:
  197. /* load up the packet */
  198. if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
  199. return AVERROR(EIO);
  200. /* copy over preamble */
  201. memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
  202. if (chunk_type == RoQ_QUAD_VQ) {
  203. pkt->stream_index = roq->video_stream_index;
  204. pkt->pts = roq->video_pts;
  205. roq->video_pts += roq->frame_pts_inc;
  206. } else {
  207. pkt->stream_index = roq->audio_stream_index;
  208. pkt->pts = roq->audio_frame_count;
  209. pkt->pts *= 90000;
  210. pkt->pts /= RoQ_AUDIO_SAMPLE_RATE;
  211. roq->audio_frame_count += (chunk_size / roq->audio_channels);
  212. }
  213. pkt->pos= url_ftell(pb);
  214. ret = get_buffer(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
  215. chunk_size);
  216. if (ret != chunk_size)
  217. ret = AVERROR(EIO);
  218. packet_read = 1;
  219. break;
  220. default:
  221. av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
  222. return AVERROR_INVALIDDATA;
  223. break;
  224. }
  225. }
  226. return ret;
  227. }
  228. static int roq_read_close(AVFormatContext *s)
  229. {
  230. // RoqDemuxContext *roq = s->priv_data;
  231. return 0;
  232. }
  233. AVInputFormat roq_demuxer = {
  234. "RoQ",
  235. "Id RoQ format",
  236. sizeof(RoqDemuxContext),
  237. roq_probe,
  238. roq_read_header,
  239. roq_read_packet,
  240. roq_read_close,
  241. };