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.

252 lines
7.2KB

  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;
  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. avi = malloc(sizeof(AVIContext));
  52. if (!avi)
  53. return -1;
  54. memset(avi, 0, sizeof(AVIContext));
  55. s->priv_data = avi;
  56. /* check RIFF header */
  57. tag = get_le32(pb);
  58. if (tag != MKTAG('R', 'I', 'F', 'F'))
  59. return -1;
  60. get_le32(pb); /* file size */
  61. tag = get_le32(pb);
  62. if (tag != MKTAG('A', 'V', 'I', ' '))
  63. return -1;
  64. /* first list tag */
  65. stream_index = -1;
  66. codec_type = -1;
  67. frame_period = 0;
  68. for(;;) {
  69. if (url_feof(pb))
  70. goto fail;
  71. tag = get_le32(pb);
  72. size = get_le32(pb);
  73. #ifdef DEBUG
  74. print_tag("tag", tag, size);
  75. #endif
  76. switch(tag) {
  77. case MKTAG('L', 'I', 'S', 'T'):
  78. /* ignored, except when start of video packets */
  79. tag1 = get_le32(pb);
  80. #ifdef DEBUG
  81. print_tag("list", tag1, 0);
  82. #endif
  83. if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
  84. avi->movi_end = url_ftell(pb) + size - 4;
  85. #ifdef DEBUG
  86. printf("movi end=%Lx\n", avi->movi_end);
  87. #endif
  88. goto end_of_header;
  89. }
  90. break;
  91. case MKTAG('a', 'v', 'i', 'h'):
  92. /* avi header */
  93. frame_period = get_le32(pb);
  94. bit_rate = get_le32(pb) * 8;
  95. url_fskip(pb, 4 * 4);
  96. s->nb_streams = get_le32(pb);
  97. for(i=0;i<s->nb_streams;i++) {
  98. AVStream *st;
  99. st = malloc(sizeof(AVStream));
  100. if (!st)
  101. goto fail;
  102. memset(st, 0, sizeof(AVStream));
  103. s->streams[i] = st;
  104. }
  105. url_fskip(pb, size - 7 * 4);
  106. break;
  107. case MKTAG('s', 't', 'r', 'h'):
  108. /* stream header */
  109. stream_index++;
  110. tag1 = get_le32(pb);
  111. switch(tag1) {
  112. case MKTAG('v', 'i', 'd', 's'):
  113. codec_type = CODEC_TYPE_VIDEO;
  114. get_le32(pb); /* codec tag */
  115. get_le32(pb); /* flags */
  116. get_le16(pb); /* priority */
  117. get_le16(pb); /* language */
  118. get_le32(pb); /* XXX: initial frame ? */
  119. get_le32(pb); /* scale */
  120. get_le32(pb); /* rate */
  121. url_fskip(pb, size - 7 * 4);
  122. break;
  123. case MKTAG('a', 'u', 'd', 's'):
  124. codec_type = CODEC_TYPE_AUDIO;
  125. /* nothing really useful */
  126. url_fskip(pb, size - 4);
  127. break;
  128. default:
  129. goto fail;
  130. }
  131. break;
  132. case MKTAG('s', 't', 'r', 'f'):
  133. /* stream header */
  134. if (stream_index >= s->nb_streams) {
  135. url_fskip(pb, size);
  136. } else {
  137. st = s->streams[stream_index];
  138. switch(codec_type) {
  139. case CODEC_TYPE_VIDEO:
  140. get_le32(pb); /* size */
  141. st->codec.width = get_le32(pb);
  142. st->codec.height = get_le32(pb);
  143. if (frame_period)
  144. st->codec.frame_rate = (INT64_C(1000000) * FRAME_RATE_BASE) / frame_period;
  145. else
  146. st->codec.frame_rate = 25 * FRAME_RATE_BASE;
  147. get_le16(pb); /* panes */
  148. get_le16(pb); /* depth */
  149. tag1 = get_le32(pb);
  150. #ifdef DEBUG
  151. print_tag("video", tag1, 0);
  152. #endif
  153. st->codec.codec_type = CODEC_TYPE_VIDEO;
  154. st->codec.codec_tag = tag1;
  155. st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
  156. url_fskip(pb, size - 5 * 4);
  157. break;
  158. case CODEC_TYPE_AUDIO:
  159. tag1 = get_le16(pb);
  160. st->codec.codec_type = CODEC_TYPE_AUDIO;
  161. st->codec.codec_tag = tag1;
  162. st->codec.codec_id = codec_get_id(codec_wav_tags, tag1);
  163. #ifdef DEBUG
  164. printf("audio: 0x%x\n", tag1);
  165. #endif
  166. st->codec.channels = get_le16(pb);
  167. st->codec.sample_rate = get_le32(pb);
  168. st->codec.bit_rate = get_le32(pb) * 8;
  169. url_fskip(pb, size - 3 * 4);
  170. break;
  171. default:
  172. url_fskip(pb, size);
  173. break;
  174. }
  175. }
  176. break;
  177. default:
  178. /* skip tag */
  179. size += (size & 1);
  180. url_fskip(pb, size);
  181. break;
  182. }
  183. }
  184. end_of_header:
  185. /* check stream number */
  186. if (stream_index != s->nb_streams - 1) {
  187. fail:
  188. for(i=0;i<s->nb_streams;i++) {
  189. if (s->streams[i])
  190. free(s->streams[i]);
  191. }
  192. return -1;
  193. }
  194. return 0;
  195. }
  196. int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  197. {
  198. AVIContext *avi = s->priv_data;
  199. ByteIOContext *pb = &s->pb;
  200. int n, d1, d2, size;
  201. find_next:
  202. if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
  203. return -1;
  204. d1 = get_byte(pb);
  205. if (d1 < '0' || d1 > '9')
  206. goto find_next;
  207. d2 = get_byte(pb);
  208. if (d2 < '0' || d2 > '9')
  209. goto find_next;
  210. n = (d1 - '0') * 10 + (d2 - '0');
  211. if (n < 0 || n >= s->nb_streams)
  212. goto find_next;
  213. d1 = get_byte(pb);
  214. d2 = get_byte(pb);
  215. if ((d1 != 'd' && d2 != 'c') &&
  216. (d1 != 'w' && d2 != 'b'))
  217. goto find_next;
  218. size = get_le32(pb);
  219. av_new_packet(pkt, size);
  220. pkt->stream_index = n;
  221. get_buffer(pb, pkt->data, pkt->size);
  222. if (size & 1)
  223. get_byte(pb);
  224. return 0;
  225. }
  226. int avi_read_close(AVFormatContext *s)
  227. {
  228. AVIContext *avi = s->priv_data;
  229. free(avi);
  230. return 0;
  231. }