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.

257 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. s->streams[i] = st;
  98. }
  99. url_fskip(pb, size - 7 * 4);
  100. break;
  101. case MKTAG('s', 't', 'r', 'h'):
  102. /* stream header */
  103. stream_index++;
  104. tag1 = get_le32(pb);
  105. switch(tag1) {
  106. case MKTAG('v', 'i', 'd', 's'):
  107. codec_type = CODEC_TYPE_VIDEO;
  108. get_le32(pb); /* codec tag */
  109. get_le32(pb); /* flags */
  110. get_le16(pb); /* priority */
  111. get_le16(pb); /* language */
  112. get_le32(pb); /* XXX: initial frame ? */
  113. get_le32(pb); /* scale */
  114. get_le32(pb); /* rate */
  115. size -= 6 * 4;
  116. break;
  117. case MKTAG('a', 'u', 'd', 's'):
  118. codec_type = CODEC_TYPE_AUDIO;
  119. /* nothing really useful */
  120. }
  121. url_fskip(pb, size - 4);
  122. break;
  123. case MKTAG('s', 't', 'r', 'f'):
  124. /* stream header */
  125. if (stream_index >= s->nb_streams) {
  126. url_fskip(pb, size);
  127. } else {
  128. st = s->streams[stream_index];
  129. switch(codec_type) {
  130. case CODEC_TYPE_VIDEO:
  131. get_le32(pb); /* size */
  132. st->codec.width = get_le32(pb);
  133. st->codec.height = get_le32(pb);
  134. if (frame_period)
  135. st->codec.frame_rate = (INT64_C(1000000) * FRAME_RATE_BASE) / frame_period;
  136. else
  137. st->codec.frame_rate = 25 * FRAME_RATE_BASE;
  138. get_le16(pb); /* panes */
  139. get_le16(pb); /* depth */
  140. tag1 = get_le32(pb);
  141. #ifdef DEBUG
  142. print_tag("video", tag1, 0);
  143. #endif
  144. st->codec.codec_type = CODEC_TYPE_VIDEO;
  145. st->codec.codec_tag = tag1;
  146. st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
  147. url_fskip(pb, size - 5 * 4);
  148. break;
  149. case CODEC_TYPE_AUDIO:
  150. get_wav_header(pb, &st->codec, (size >= 18));
  151. break;
  152. default:
  153. url_fskip(pb, size);
  154. break;
  155. }
  156. }
  157. break;
  158. default:
  159. /* skip tag */
  160. size += (size & 1);
  161. url_fskip(pb, size);
  162. break;
  163. }
  164. }
  165. end_of_header:
  166. /* check stream number */
  167. if (stream_index != s->nb_streams - 1) {
  168. fail:
  169. for(i=0;i<s->nb_streams;i++) {
  170. av_freep(&s->streams[i]);
  171. }
  172. return -1;
  173. }
  174. return 0;
  175. }
  176. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  177. {
  178. AVIContext *avi = s->priv_data;
  179. ByteIOContext *pb = &s->pb;
  180. int n, d1, d2, size;
  181. for(;;) {
  182. if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
  183. return -1;
  184. d1 = get_byte(pb) - '0';
  185. d2 = get_byte(pb) - '0';
  186. if (d1 < 0 || d1 > 9 || d2 < 0 || d2 > 9)
  187. continue;
  188. n = d1 * 10 + d2;
  189. if (n < 0 || n >= s->nb_streams)
  190. continue;
  191. d1 = get_byte(pb);
  192. d2 = get_byte(pb);
  193. if ((d1 == 'd' && d2 == 'c')
  194. || (d1 == 'w' && d2 == 'b'))
  195. break;
  196. }
  197. size = get_le32(pb);
  198. av_new_packet(pkt, size);
  199. pkt->stream_index = n;
  200. get_buffer(pb, pkt->data, pkt->size);
  201. if (size & 1)
  202. get_byte(pb);
  203. return 0;
  204. }
  205. static int avi_read_close(AVFormatContext *s)
  206. {
  207. return 0;
  208. }
  209. static int avi_probe(AVProbeData *p)
  210. {
  211. /* check file header */
  212. if (p->buf_size <= 32)
  213. return 0;
  214. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  215. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  216. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  217. p->buf[10] == 'I' && p->buf[11] == ' ')
  218. return AVPROBE_SCORE_MAX;
  219. else
  220. return 0;
  221. }
  222. static AVInputFormat avi_iformat = {
  223. "avi",
  224. "avi format",
  225. sizeof(AVIContext),
  226. avi_probe,
  227. avi_read_header,
  228. avi_read_packet,
  229. avi_read_close,
  230. };
  231. int avidec_init(void)
  232. {
  233. av_register_input_format(&avi_iformat);
  234. return 0;
  235. }