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.

296 lines
8.7KB

  1. /*
  2. * Id RoQ (.roq) File Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file idroq.c
  21. * Id RoQ format file demuxer
  22. * by Mike Melanson (melanson@pcisys.net)
  23. * for more information on the .roq file format, visit:
  24. * http://www.csse.monash.edu.au/~timf/
  25. */
  26. #include "avformat.h"
  27. #define RoQ_MAGIC_NUMBER 0x1084
  28. #define RoQ_CHUNK_PREAMBLE_SIZE 8
  29. #define RoQ_AUDIO_SAMPLE_RATE 22050
  30. #define RoQ_CHUNKS_TO_SCAN 30
  31. #define RoQ_INFO 0x1001
  32. #define RoQ_QUAD_CODEBOOK 0x1002
  33. #define RoQ_QUAD_VQ 0x1011
  34. #define RoQ_SOUND_MONO 0x1020
  35. #define RoQ_SOUND_STEREO 0x1021
  36. typedef struct RoqDemuxContext {
  37. int width;
  38. int height;
  39. int audio_channels;
  40. int framerate;
  41. int frame_pts_inc;
  42. int video_stream_index;
  43. int audio_stream_index;
  44. int64_t video_pts;
  45. unsigned int audio_frame_count;
  46. } RoqDemuxContext;
  47. static int roq_probe(AVProbeData *p)
  48. {
  49. if (p->buf_size < 6)
  50. return 0;
  51. if ((LE_16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
  52. (LE_32(&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_IO;
  70. roq->framerate = LE_16(&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_IO;
  80. chunk_type = LE_16(&preamble[0]);
  81. chunk_size = LE_32(&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_IO;
  88. roq->width = LE_16(&preamble[0]);
  89. roq->height = LE_16(&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", LE_16(&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_NOMEM;
  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_NOMEM;
  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_IO;
  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_IO;
  164. chunk_type = LE_16(&preamble[0]);
  165. chunk_size = LE_32(&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_IO;
  181. chunk_size = LE_32(&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_IO;
  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_IO;
  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_IO;
  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 = (RoqDemuxContext *)s->priv_data;
  231. return 0;
  232. }
  233. static AVInputFormat roq_iformat = {
  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. };
  242. int roq_init(void)
  243. {
  244. av_register_input_format(&roq_iformat);
  245. return 0;
  246. }