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.

259 lines
7.0KB

  1. /*
  2. * AVI decoder.
  3. * Copyright (c) 2001 Fabrice Bellard.
  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. #include "avformat.h"
  20. #include "avi.h"
  21. //#define DEBUG
  22. typedef struct AVIIndex {
  23. unsigned char tag[4];
  24. unsigned int flags, pos, len;
  25. struct AVIIndex *next;
  26. } AVIIndex;
  27. typedef struct {
  28. INT64 movi_end;
  29. offset_t movi_list;
  30. AVIIndex *first, *last;
  31. } AVIContext;
  32. #ifdef DEBUG
  33. static void print_tag(const char *str, unsigned int tag, int size)
  34. {
  35. printf("%s: tag=%c%c%c%c size=0x%x\n",
  36. str, tag & 0xff,
  37. (tag >> 8) & 0xff,
  38. (tag >> 16) & 0xff,
  39. (tag >> 24) & 0xff,
  40. size);
  41. }
  42. #endif
  43. static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
  44. {
  45. AVIContext *avi = s->priv_data;
  46. ByteIOContext *pb = &s->pb;
  47. UINT32 tag, tag1;
  48. int codec_type, stream_index, size, frame_period, bit_rate;
  49. int i;
  50. AVStream *st;
  51. /* check RIFF header */
  52. tag = get_le32(pb);
  53. if (tag != MKTAG('R', 'I', 'F', 'F'))
  54. return -1;
  55. get_le32(pb); /* file size */
  56. tag = get_le32(pb);
  57. if (tag != MKTAG('A', 'V', 'I', ' '))
  58. return -1;
  59. /* first list tag */
  60. stream_index = -1;
  61. codec_type = -1;
  62. frame_period = 0;
  63. for(;;) {
  64. if (url_feof(pb))
  65. goto fail;
  66. tag = get_le32(pb);
  67. size = get_le32(pb);
  68. #ifdef DEBUG
  69. print_tag("tag", tag, size);
  70. #endif
  71. switch(tag) {
  72. case MKTAG('L', 'I', 'S', 'T'):
  73. /* ignored, except when start of video packets */
  74. tag1 = get_le32(pb);
  75. #ifdef DEBUG
  76. print_tag("list", tag1, 0);
  77. #endif
  78. if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
  79. avi->movi_end = url_ftell(pb) + size - 4;
  80. #ifdef DEBUG
  81. printf("movi end=%Lx\n", avi->movi_end);
  82. #endif
  83. goto end_of_header;
  84. }
  85. break;
  86. case MKTAG('a', 'v', 'i', 'h'):
  87. /* avi header */
  88. /* using frame_period is bad idea */
  89. frame_period = get_le32(pb);
  90. bit_rate = get_le32(pb) * 8;
  91. url_fskip(pb, 4 * 4);
  92. s->nb_streams = get_le32(pb);
  93. for(i=0;i<s->nb_streams;i++) {
  94. AVStream *st = av_mallocz(sizeof(AVStream));
  95. if (!st)
  96. goto fail;
  97. avcodec_get_context_defaults(&st->codec);
  98. s->streams[i] = st;
  99. }
  100. url_fskip(pb, size - 7 * 4);
  101. break;
  102. case MKTAG('s', 't', 'r', 'h'):
  103. /* stream header */
  104. stream_index++;
  105. tag1 = get_le32(pb);
  106. switch(tag1) {
  107. case MKTAG('v', 'i', 'd', 's'):
  108. codec_type = CODEC_TYPE_VIDEO;
  109. get_le32(pb); /* codec tag */
  110. get_le32(pb); /* flags */
  111. get_le16(pb); /* priority */
  112. get_le16(pb); /* language */
  113. get_le32(pb); /* XXX: initial frame ? */
  114. get_le32(pb); /* scale */
  115. get_le32(pb); /* rate */
  116. size -= 6 * 4;
  117. break;
  118. case MKTAG('a', 'u', 'd', 's'):
  119. codec_type = CODEC_TYPE_AUDIO;
  120. /* nothing really useful */
  121. }
  122. url_fskip(pb, size - 4);
  123. break;
  124. case MKTAG('s', 't', 'r', 'f'):
  125. /* stream header */
  126. if (stream_index >= s->nb_streams) {
  127. url_fskip(pb, size);
  128. } else {
  129. st = s->streams[stream_index];
  130. switch(codec_type) {
  131. case CODEC_TYPE_VIDEO:
  132. get_le32(pb); /* size */
  133. st->codec.width = get_le32(pb);
  134. st->codec.height = get_le32(pb);
  135. if (frame_period)
  136. st->codec.frame_rate = (INT64_C(1000000) * FRAME_RATE_BASE) / frame_period;
  137. else
  138. st->codec.frame_rate = 25 * FRAME_RATE_BASE;
  139. get_le16(pb); /* panes */
  140. get_le16(pb); /* depth */
  141. tag1 = get_le32(pb);
  142. #ifdef DEBUG
  143. print_tag("video", tag1, 0);
  144. #endif
  145. st->codec.codec_type = CODEC_TYPE_VIDEO;
  146. st->codec.codec_tag = tag1;
  147. st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
  148. url_fskip(pb, size - 5 * 4);
  149. break;
  150. case CODEC_TYPE_AUDIO:
  151. get_wav_header(pb, &st->codec, (size >= 18));
  152. break;
  153. default:
  154. url_fskip(pb, size);
  155. break;
  156. }
  157. }
  158. break;
  159. default:
  160. /* skip tag */
  161. size += (size & 1);
  162. url_fskip(pb, size);
  163. break;
  164. }
  165. }
  166. end_of_header:
  167. /* check stream number */
  168. if (stream_index != s->nb_streams - 1) {
  169. fail:
  170. for(i=0;i<s->nb_streams;i++) {
  171. av_freep(&s->streams[i]);
  172. }
  173. return -1;
  174. }
  175. return 0;
  176. }
  177. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  178. {
  179. AVIContext *avi = s->priv_data;
  180. ByteIOContext *pb = &s->pb;
  181. int n, d1, d2, size;
  182. for(;;) {
  183. if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
  184. return -1;
  185. d1 = get_byte(pb) - '0';
  186. d2 = get_byte(pb) - '0';
  187. if (d1 < 0 || d1 > 9 || d2 < 0 || d2 > 9)
  188. continue;
  189. n = d1 * 10 + d2;
  190. if (n < 0 || n >= s->nb_streams)
  191. continue;
  192. d1 = get_byte(pb);
  193. d2 = get_byte(pb);
  194. if ((d1 == 'd' && d2 == 'c')
  195. || (d1 == 'w' && d2 == 'b'))
  196. break;
  197. }
  198. size = get_le32(pb);
  199. av_new_packet(pkt, size);
  200. pkt->stream_index = n;
  201. get_buffer(pb, pkt->data, pkt->size);
  202. if (size & 1)
  203. get_byte(pb);
  204. return 0;
  205. }
  206. static int avi_read_close(AVFormatContext *s)
  207. {
  208. return 0;
  209. }
  210. static int avi_probe(AVProbeData *p)
  211. {
  212. /* check file header */
  213. if (p->buf_size <= 32)
  214. return 0;
  215. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  216. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  217. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  218. p->buf[10] == 'I' && p->buf[11] == ' ')
  219. return AVPROBE_SCORE_MAX;
  220. else
  221. return 0;
  222. }
  223. static AVInputFormat avi_iformat = {
  224. "avi",
  225. "avi format",
  226. sizeof(AVIContext),
  227. avi_probe,
  228. avi_read_header,
  229. avi_read_packet,
  230. avi_read_close,
  231. };
  232. int avidec_init(void)
  233. {
  234. av_register_input_format(&avi_iformat);
  235. return 0;
  236. }