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.

242 lines
8.0KB

  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/channel_layout.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "avformat.h"
  31. #include "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 frame_rate;
  43. int width;
  44. int height;
  45. int audio_channels;
  46. int video_stream_index;
  47. int audio_stream_index;
  48. int64_t video_pts;
  49. unsigned int audio_frame_count;
  50. } RoqDemuxContext;
  51. static int roq_probe(AVProbeData *p)
  52. {
  53. if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
  54. (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
  55. return 0;
  56. return AVPROBE_SCORE_MAX;
  57. }
  58. static int roq_read_header(AVFormatContext *s)
  59. {
  60. RoqDemuxContext *roq = s->priv_data;
  61. AVIOContext *pb = s->pb;
  62. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  63. /* get the main header */
  64. if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  65. RoQ_CHUNK_PREAMBLE_SIZE)
  66. return AVERROR(EIO);
  67. roq->frame_rate = AV_RL16(&preamble[6]);
  68. /* init private context parameters */
  69. roq->width = roq->height = roq->audio_channels = roq->video_pts =
  70. roq->audio_frame_count = 0;
  71. roq->audio_stream_index = -1;
  72. roq->video_stream_index = -1;
  73. s->ctx_flags |= AVFMTCTX_NOHEADER;
  74. return 0;
  75. }
  76. static int roq_read_packet(AVFormatContext *s,
  77. AVPacket *pkt)
  78. {
  79. RoqDemuxContext *roq = s->priv_data;
  80. AVIOContext *pb = s->pb;
  81. int ret = 0;
  82. unsigned int chunk_size;
  83. unsigned int chunk_type;
  84. unsigned int codebook_size;
  85. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  86. int packet_read = 0;
  87. int64_t codebook_offset;
  88. while (!packet_read) {
  89. if (s->pb->eof_reached)
  90. return AVERROR(EIO);
  91. /* get the next chunk preamble */
  92. if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
  93. RoQ_CHUNK_PREAMBLE_SIZE)
  94. return AVERROR(EIO);
  95. chunk_type = AV_RL16(&preamble[0]);
  96. chunk_size = AV_RL32(&preamble[2]);
  97. if(chunk_size > INT_MAX)
  98. return AVERROR_INVALIDDATA;
  99. switch (chunk_type) {
  100. case RoQ_INFO:
  101. if (roq->video_stream_index == -1) {
  102. AVStream *st = avformat_new_stream(s, NULL);
  103. if (!st)
  104. return AVERROR(ENOMEM);
  105. avpriv_set_pts_info(st, 63, 1, roq->frame_rate);
  106. roq->video_stream_index = st->index;
  107. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  108. st->codecpar->codec_id = AV_CODEC_ID_ROQ;
  109. st->codecpar->codec_tag = 0; /* no fourcc */
  110. if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE)
  111. return AVERROR(EIO);
  112. st->codecpar->width = roq->width = AV_RL16(preamble);
  113. st->codecpar->height = roq->height = AV_RL16(preamble + 2);
  114. break;
  115. }
  116. /* don't care about this chunk anymore */
  117. avio_skip(pb, RoQ_CHUNK_PREAMBLE_SIZE);
  118. break;
  119. case RoQ_QUAD_CODEBOOK:
  120. if (roq->video_stream_index < 0)
  121. return AVERROR_INVALIDDATA;
  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->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  150. st->codecpar->codec_id = AV_CODEC_ID_ROQ_DPCM;
  151. st->codecpar->codec_tag = 0; /* no tag */
  152. if (chunk_type == RoQ_SOUND_STEREO) {
  153. st->codecpar->channels = 2;
  154. st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  155. } else {
  156. st->codecpar->channels = 1;
  157. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  158. }
  159. roq->audio_channels = st->codecpar->channels;
  160. st->codecpar->sample_rate = RoQ_AUDIO_SAMPLE_RATE;
  161. st->codecpar->bits_per_coded_sample = 16;
  162. st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate *
  163. st->codecpar->bits_per_coded_sample;
  164. st->codecpar->block_align = st->codecpar->channels * st->codecpar->bits_per_coded_sample;
  165. }
  166. case RoQ_QUAD_VQ:
  167. if (chunk_type == RoQ_QUAD_VQ) {
  168. if (roq->video_stream_index < 0)
  169. return AVERROR_INVALIDDATA;
  170. }
  171. /* load up the packet */
  172. if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
  173. return AVERROR(EIO);
  174. /* copy over preamble */
  175. memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
  176. if (chunk_type == RoQ_QUAD_VQ) {
  177. pkt->stream_index = roq->video_stream_index;
  178. pkt->pts = roq->video_pts++;
  179. } else {
  180. pkt->stream_index = roq->audio_stream_index;
  181. pkt->pts = roq->audio_frame_count;
  182. roq->audio_frame_count += (chunk_size / roq->audio_channels);
  183. }
  184. pkt->pos= avio_tell(pb);
  185. ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
  186. chunk_size);
  187. if (ret != chunk_size)
  188. ret = AVERROR(EIO);
  189. packet_read = 1;
  190. break;
  191. default:
  192. av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
  193. return AVERROR_INVALIDDATA;
  194. }
  195. }
  196. return ret;
  197. }
  198. AVInputFormat ff_roq_demuxer = {
  199. .name = "roq",
  200. .long_name = NULL_IF_CONFIG_SMALL("id RoQ"),
  201. .priv_data_size = sizeof(RoqDemuxContext),
  202. .read_probe = roq_probe,
  203. .read_header = roq_read_header,
  204. .read_packet = roq_read_packet,
  205. };