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.

303 lines
8.9KB

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