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.

275 lines
7.7KB

  1. /*
  2. * AVI decoder.
  3. * Copyright (c) 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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. 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. 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, bps;
  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. frame_period = get_le32(pb);
  89. bit_rate = get_le32(pb) * 8;
  90. url_fskip(pb, 4 * 4);
  91. s->nb_streams = get_le32(pb);
  92. for(i=0;i<s->nb_streams;i++) {
  93. AVStream *st;
  94. st = av_malloc(sizeof(AVStream));
  95. if (!st)
  96. goto fail;
  97. memset(st, 0, sizeof(AVStream));
  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. url_fskip(pb, size - 7 * 4);
  117. break;
  118. case MKTAG('a', 'u', 'd', 's'):
  119. codec_type = CODEC_TYPE_AUDIO;
  120. /* nothing really useful */
  121. url_fskip(pb, size - 4);
  122. break;
  123. default:
  124. goto fail;
  125. }
  126. break;
  127. case MKTAG('s', 't', 'r', 'f'):
  128. /* stream header */
  129. if (stream_index >= s->nb_streams) {
  130. url_fskip(pb, size);
  131. } else {
  132. st = s->streams[stream_index];
  133. switch(codec_type) {
  134. case CODEC_TYPE_VIDEO:
  135. get_le32(pb); /* size */
  136. st->codec.width = get_le32(pb);
  137. st->codec.height = get_le32(pb);
  138. if (frame_period)
  139. st->codec.frame_rate = (INT64_C(1000000) * FRAME_RATE_BASE) / frame_period;
  140. else
  141. st->codec.frame_rate = 25 * FRAME_RATE_BASE;
  142. get_le16(pb); /* panes */
  143. get_le16(pb); /* depth */
  144. tag1 = get_le32(pb);
  145. #ifdef DEBUG
  146. print_tag("video", tag1, 0);
  147. #endif
  148. st->codec.codec_type = CODEC_TYPE_VIDEO;
  149. st->codec.codec_tag = tag1;
  150. st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
  151. url_fskip(pb, size - 5 * 4);
  152. break;
  153. case CODEC_TYPE_AUDIO:
  154. tag1 = get_le16(pb);
  155. st->codec.codec_type = CODEC_TYPE_AUDIO;
  156. st->codec.codec_tag = tag1;
  157. #ifdef DEBUG
  158. printf("audio: 0x%x\n", tag1);
  159. #endif
  160. st->codec.channels = get_le16(pb);
  161. st->codec.sample_rate = get_le32(pb);
  162. st->codec.bit_rate = get_le32(pb) * 8;
  163. get_le16(pb); /* block align */
  164. bps = get_le16(pb);
  165. st->codec.codec_id = wav_codec_get_id(tag1, bps);
  166. url_fskip(pb, size - 4 * 4);
  167. break;
  168. default:
  169. url_fskip(pb, size);
  170. break;
  171. }
  172. }
  173. break;
  174. default:
  175. /* skip tag */
  176. size += (size & 1);
  177. url_fskip(pb, size);
  178. break;
  179. }
  180. }
  181. end_of_header:
  182. /* check stream number */
  183. if (stream_index != s->nb_streams - 1) {
  184. fail:
  185. for(i=0;i<s->nb_streams;i++) {
  186. av_freep(&s->streams[i]);
  187. }
  188. return -1;
  189. }
  190. return 0;
  191. }
  192. int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  193. {
  194. AVIContext *avi = s->priv_data;
  195. ByteIOContext *pb = &s->pb;
  196. int n, d1, d2, size;
  197. find_next:
  198. if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
  199. return -1;
  200. d1 = get_byte(pb);
  201. if (d1 < '0' || d1 > '9')
  202. goto find_next;
  203. d2 = get_byte(pb);
  204. if (d2 < '0' || d2 > '9')
  205. goto find_next;
  206. n = (d1 - '0') * 10 + (d2 - '0');
  207. if (n < 0 || n >= s->nb_streams)
  208. goto find_next;
  209. d1 = get_byte(pb);
  210. d2 = get_byte(pb);
  211. if ((d1 != 'd' && d2 != 'c') &&
  212. (d1 != 'w' && d2 != 'b'))
  213. goto find_next;
  214. size = get_le32(pb);
  215. av_new_packet(pkt, size);
  216. pkt->stream_index = n;
  217. get_buffer(pb, pkt->data, pkt->size);
  218. if (size & 1)
  219. get_byte(pb);
  220. return 0;
  221. }
  222. int avi_read_close(AVFormatContext *s)
  223. {
  224. return 0;
  225. }
  226. static int avi_probe(AVProbeData *p)
  227. {
  228. /* check file header */
  229. if (p->buf_size <= 32)
  230. return 0;
  231. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  232. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  233. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  234. p->buf[10] == 'I' && p->buf[11] == ' ')
  235. return AVPROBE_SCORE_MAX;
  236. else
  237. return 0;
  238. }
  239. static AVInputFormat avi_iformat = {
  240. "avi",
  241. "avi format",
  242. sizeof(AVIContext),
  243. avi_probe,
  244. avi_read_header,
  245. avi_read_packet,
  246. avi_read_close,
  247. };
  248. int avidec_init(void)
  249. {
  250. av_register_input_format(&avi_iformat);
  251. return 0;
  252. }