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.

311 lines
9.1KB

  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_t 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_t tag, tag1;
  48. int codec_type, stream_index, frame_period, bit_rate, scale, rate;
  49. unsigned int size;
  50. int i;
  51. AVStream *st;
  52. /* check RIFF header */
  53. tag = get_le32(pb);
  54. if (tag != MKTAG('R', 'I', 'F', 'F'))
  55. return -1;
  56. get_le32(pb); /* file size */
  57. tag = get_le32(pb);
  58. if (tag != MKTAG('A', 'V', 'I', ' '))
  59. return -1;
  60. /* first list tag */
  61. stream_index = -1;
  62. codec_type = -1;
  63. frame_period = 0;
  64. for(;;) {
  65. if (url_feof(pb))
  66. goto fail;
  67. tag = get_le32(pb);
  68. size = get_le32(pb);
  69. #ifdef DEBUG
  70. print_tag("tag", tag, size);
  71. #endif
  72. switch(tag) {
  73. case MKTAG('L', 'I', 'S', 'T'):
  74. /* ignored, except when start of video packets */
  75. tag1 = get_le32(pb);
  76. #ifdef DEBUG
  77. print_tag("list", tag1, 0);
  78. #endif
  79. if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
  80. avi->movi_end = url_ftell(pb) + size - 4;
  81. #ifdef DEBUG
  82. printf("movi end=%Lx\n", avi->movi_end);
  83. #endif
  84. goto end_of_header;
  85. }
  86. break;
  87. case MKTAG('a', 'v', 'i', 'h'):
  88. /* avi header */
  89. /* using frame_period is bad idea */
  90. frame_period = get_le32(pb);
  91. bit_rate = get_le32(pb) * 8;
  92. url_fskip(pb, 4 * 4);
  93. s->nb_streams = get_le32(pb);
  94. for(i=0;i<s->nb_streams;i++) {
  95. AVStream *st = av_mallocz(sizeof(AVStream));
  96. if (!st)
  97. goto fail;
  98. avcodec_get_context_defaults(&st->codec);
  99. s->streams[i] = st;
  100. }
  101. url_fskip(pb, size - 7 * 4);
  102. break;
  103. case MKTAG('s', 't', 'r', 'h'):
  104. /* stream header */
  105. stream_index++;
  106. tag1 = get_le32(pb);
  107. switch(tag1) {
  108. case MKTAG('v', 'i', 'd', 's'):
  109. codec_type = CODEC_TYPE_VIDEO;
  110. if (stream_index >= s->nb_streams) {
  111. url_fskip(pb, size - 4);
  112. break;
  113. }
  114. st = s->streams[stream_index];
  115. get_le32(pb); /* codec tag */
  116. get_le32(pb); /* flags */
  117. get_le16(pb); /* priority */
  118. get_le16(pb); /* language */
  119. get_le32(pb); /* XXX: initial frame ? */
  120. scale= get_le32(pb); /* scale */
  121. rate= get_le32(pb); /* rate */
  122. if(scale && rate){
  123. st->codec.frame_rate = rate;
  124. st->codec.frame_rate_base= scale;
  125. }else if(frame_period){
  126. st->codec.frame_rate = 1000000;
  127. st->codec.frame_rate_base= frame_period;
  128. }else{
  129. st->codec.frame_rate = 25;
  130. st->codec.frame_rate_base = 1;
  131. }
  132. url_fskip(pb, size - 7 * 4);
  133. break;
  134. case MKTAG('a', 'u', 'd', 's'):
  135. codec_type = CODEC_TYPE_AUDIO;
  136. /* nothing really useful */
  137. url_fskip(pb, size - 4);
  138. break;
  139. default:
  140. goto fail;
  141. }
  142. break;
  143. case MKTAG('s', 't', 'r', 'f'):
  144. /* stream header */
  145. if (stream_index >= s->nb_streams) {
  146. url_fskip(pb, size);
  147. } else {
  148. st = s->streams[stream_index];
  149. switch(codec_type) {
  150. case CODEC_TYPE_VIDEO:
  151. get_le32(pb); /* size */
  152. st->codec.width = get_le32(pb);
  153. st->codec.height = get_le32(pb);
  154. get_le16(pb); /* panes */
  155. st->codec.bits_per_sample= get_le16(pb); /* depth */
  156. tag1 = get_le32(pb);
  157. get_le32(pb); /* ImageSize */
  158. get_le32(pb); /* XPelsPerMeter */
  159. get_le32(pb); /* YPelsPerMeter */
  160. get_le32(pb); /* ClrUsed */
  161. get_le32(pb); /* ClrImportant */
  162. st->codec.extradata_size= size - 10*4;
  163. st->codec.extradata= av_malloc(st->codec.extradata_size); //FIXME where should we free this?
  164. get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
  165. if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
  166. get_byte(pb);
  167. #ifdef DEBUG
  168. print_tag("video", tag1, 0);
  169. #endif
  170. st->codec.codec_type = CODEC_TYPE_VIDEO;
  171. st->codec.codec_tag = tag1;
  172. st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
  173. // url_fskip(pb, size - 5 * 4);
  174. break;
  175. case CODEC_TYPE_AUDIO:
  176. get_wav_header(pb, &st->codec, size);
  177. if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
  178. url_fskip(pb, 1);
  179. break;
  180. default:
  181. url_fskip(pb, size);
  182. break;
  183. }
  184. }
  185. break;
  186. default:
  187. /* skip tag */
  188. size += (size & 1);
  189. url_fskip(pb, size);
  190. break;
  191. }
  192. }
  193. end_of_header:
  194. /* check stream number */
  195. if (stream_index != s->nb_streams - 1) {
  196. fail:
  197. for(i=0;i<s->nb_streams;i++) {
  198. av_freep(&s->streams[i]->codec.extradata);
  199. av_freep(&s->streams[i]);
  200. }
  201. return -1;
  202. }
  203. return 0;
  204. }
  205. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  206. {
  207. AVIContext *avi = s->priv_data;
  208. ByteIOContext *pb = &s->pb;
  209. int n, d[8], size, i;
  210. memset(d, -1, sizeof(int)*8);
  211. for(i=url_ftell(pb); (!url_feof(pb)) && i < avi->movi_end; i++){
  212. int j;
  213. for(j=0; j<7; j++)
  214. d[j]= d[j+1];
  215. d[7]= get_byte(pb);
  216. size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
  217. //parse ix##
  218. n= (d[2] - '0') * 10 + (d[3] - '0');
  219. if( d[2] >= '0' && d[2] <= '9'
  220. && d[3] >= '0' && d[3] <= '9'
  221. && d[0] == 'i' && d[1] == 'x'
  222. && n < s->nb_streams
  223. && i + size <= avi->movi_end){
  224. url_fskip(pb, size);
  225. }
  226. //parse ##dc/##wb
  227. n= (d[0] - '0') * 10 + (d[1] - '0');
  228. if( d[0] >= '0' && d[0] <= '9'
  229. && d[1] >= '0' && d[1] <= '9'
  230. &&((d[2] == 'd' && d[3] == 'c') || (d[2] == 'w' && d[3] == 'b') || (d[2] == 'd' && d[3] == 'b') )
  231. && n < s->nb_streams
  232. && i + size <= avi->movi_end){
  233. av_new_packet(pkt, size);
  234. pkt->stream_index = n;
  235. get_buffer(pb, pkt->data, pkt->size);
  236. if (size & 1)
  237. get_byte(pb);
  238. return 0;
  239. }
  240. }
  241. return -1;
  242. }
  243. static int avi_read_close(AVFormatContext *s)
  244. {
  245. return 0;
  246. }
  247. static int avi_probe(AVProbeData *p)
  248. {
  249. /* check file header */
  250. if (p->buf_size <= 32)
  251. return 0;
  252. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  253. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  254. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  255. p->buf[10] == 'I' && p->buf[11] == ' ')
  256. return AVPROBE_SCORE_MAX;
  257. else
  258. return 0;
  259. }
  260. static AVInputFormat avi_iformat = {
  261. "avi",
  262. "avi format",
  263. sizeof(AVIContext),
  264. avi_probe,
  265. avi_read_header,
  266. avi_read_packet,
  267. avi_read_close,
  268. };
  269. int avidec_init(void)
  270. {
  271. av_register_input_format(&avi_iformat);
  272. return 0;
  273. }