dxa.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * DXA demuxer
  3. * Copyright (c) 2007 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/intreadwrite.h"
  22. #include "avformat.h"
  23. #include "riff.h"
  24. #define DXA_EXTRA_SIZE 9
  25. typedef struct{
  26. int frames;
  27. int has_sound;
  28. int bpc;
  29. uint32_t bytes_left;
  30. int64_t wavpos, vidpos;
  31. int readvid;
  32. }DXAContext;
  33. static int dxa_probe(AVProbeData *p)
  34. {
  35. /* check file header */
  36. if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
  37. p->buf[2] == 'X' && p->buf[3] == 'A')
  38. return AVPROBE_SCORE_MAX;
  39. else
  40. return 0;
  41. }
  42. static int dxa_read_header(AVFormatContext *s, AVFormatParameters *ap)
  43. {
  44. ByteIOContext *pb = s->pb;
  45. DXAContext *c = s->priv_data;
  46. AVStream *st, *ast;
  47. uint32_t tag;
  48. int32_t fps;
  49. int w, h;
  50. int num, den;
  51. int flags;
  52. tag = get_le32(pb);
  53. if (tag != MKTAG('D', 'E', 'X', 'A'))
  54. return -1;
  55. flags = get_byte(pb);
  56. c->frames = get_be16(pb);
  57. if(!c->frames){
  58. av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
  59. return -1;
  60. }
  61. fps = get_be32(pb);
  62. if(fps > 0){
  63. den = 1000;
  64. num = fps;
  65. }else if (fps < 0){
  66. den = 100000;
  67. num = -fps;
  68. }else{
  69. den = 10;
  70. num = 1;
  71. }
  72. w = get_be16(pb);
  73. h = get_be16(pb);
  74. c->has_sound = 0;
  75. st = av_new_stream(s, 0);
  76. if (!st)
  77. return -1;
  78. // Parse WAV data header
  79. if(get_le32(pb) == MKTAG('W', 'A', 'V', 'E')){
  80. uint32_t size, fsize;
  81. c->has_sound = 1;
  82. size = get_be32(pb);
  83. c->vidpos = url_ftell(pb) + size;
  84. url_fskip(pb, 16);
  85. fsize = get_le32(pb);
  86. ast = av_new_stream(s, 0);
  87. if (!ast)
  88. return -1;
  89. get_wav_header(pb, ast->codec, fsize);
  90. // find 'data' chunk
  91. while(url_ftell(pb) < c->vidpos && !url_feof(pb)){
  92. tag = get_le32(pb);
  93. fsize = get_le32(pb);
  94. if(tag == MKTAG('d', 'a', 't', 'a')) break;
  95. url_fskip(pb, fsize);
  96. }
  97. c->bpc = (fsize + c->frames - 1) / c->frames;
  98. if(ast->codec->block_align)
  99. c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align;
  100. c->bytes_left = fsize;
  101. c->wavpos = url_ftell(pb);
  102. url_fseek(pb, c->vidpos, SEEK_SET);
  103. }
  104. /* now we are ready: build format streams */
  105. st->codec->codec_type = CODEC_TYPE_VIDEO;
  106. st->codec->codec_id = CODEC_ID_DXA;
  107. st->codec->width = w;
  108. st->codec->height = h;
  109. av_reduce(&den, &num, den, num, (1UL<<31)-1);
  110. av_set_pts_info(st, 33, num, den);
  111. /* flags & 0x80 means that image is interlaced,
  112. * flags & 0x40 means that image has double height
  113. * either way set true height
  114. */
  115. if(flags & 0xC0){
  116. st->codec->height >>= 1;
  117. }
  118. c->readvid = !c->has_sound;
  119. c->vidpos = url_ftell(pb);
  120. s->start_time = 0;
  121. s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
  122. av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
  123. return 0;
  124. }
  125. static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
  126. {
  127. DXAContext *c = s->priv_data;
  128. int ret;
  129. uint32_t size;
  130. uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
  131. int pal_size = 0;
  132. if(!c->readvid && c->has_sound && c->bytes_left){
  133. c->readvid = 1;
  134. url_fseek(s->pb, c->wavpos, SEEK_SET);
  135. size = FFMIN(c->bytes_left, c->bpc);
  136. ret = av_get_packet(s->pb, pkt, size);
  137. pkt->stream_index = 1;
  138. if(ret != size)
  139. return AVERROR(EIO);
  140. c->bytes_left -= size;
  141. c->wavpos = url_ftell(s->pb);
  142. return 0;
  143. }
  144. url_fseek(s->pb, c->vidpos, SEEK_SET);
  145. while(!url_feof(s->pb) && c->frames){
  146. get_buffer(s->pb, buf, 4);
  147. switch(AV_RL32(buf)){
  148. case MKTAG('N', 'U', 'L', 'L'):
  149. if(av_new_packet(pkt, 4 + pal_size) < 0)
  150. return AVERROR(ENOMEM);
  151. pkt->stream_index = 0;
  152. if(pal_size) memcpy(pkt->data, pal, pal_size);
  153. memcpy(pkt->data + pal_size, buf, 4);
  154. c->frames--;
  155. c->vidpos = url_ftell(s->pb);
  156. c->readvid = 0;
  157. return 0;
  158. case MKTAG('C', 'M', 'A', 'P'):
  159. pal_size = 768+4;
  160. memcpy(pal, buf, 4);
  161. get_buffer(s->pb, pal + 4, 768);
  162. break;
  163. case MKTAG('F', 'R', 'A', 'M'):
  164. get_buffer(s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
  165. size = AV_RB32(buf + 5);
  166. if(size > 0xFFFFFF){
  167. av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
  168. return -1;
  169. }
  170. if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
  171. return AVERROR(ENOMEM);
  172. memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
  173. ret = get_buffer(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
  174. if(ret != size){
  175. av_free_packet(pkt);
  176. return AVERROR(EIO);
  177. }
  178. if(pal_size) memcpy(pkt->data, pal, pal_size);
  179. pkt->stream_index = 0;
  180. c->frames--;
  181. c->vidpos = url_ftell(s->pb);
  182. c->readvid = 0;
  183. return 0;
  184. default:
  185. av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
  186. return -1;
  187. }
  188. }
  189. return AVERROR(EIO);
  190. }
  191. AVInputFormat dxa_demuxer = {
  192. "dxa",
  193. NULL_IF_CONFIG_SMALL("DXA"),
  194. sizeof(DXAContext),
  195. dxa_probe,
  196. dxa_read_header,
  197. dxa_read_packet,
  198. };