dxa.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. int w, h;
  36. if (p->buf_size < 15)
  37. return 0;
  38. w = AV_RB16(p->buf + 11);
  39. h = AV_RB16(p->buf + 13);
  40. /* check file header */
  41. if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
  42. p->buf[2] == 'X' && p->buf[3] == 'A' &&
  43. w && w <= 2048 && h && h <= 2048)
  44. return AVPROBE_SCORE_MAX;
  45. else
  46. return 0;
  47. }
  48. static int dxa_read_header(AVFormatContext *s, AVFormatParameters *ap)
  49. {
  50. AVIOContext *pb = s->pb;
  51. DXAContext *c = s->priv_data;
  52. AVStream *st, *ast;
  53. uint32_t tag;
  54. int32_t fps;
  55. int w, h;
  56. int num, den;
  57. int flags;
  58. int ret;
  59. tag = avio_rl32(pb);
  60. if (tag != MKTAG('D', 'E', 'X', 'A'))
  61. return -1;
  62. flags = avio_r8(pb);
  63. c->frames = avio_rb16(pb);
  64. if(!c->frames){
  65. av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
  66. return -1;
  67. }
  68. fps = avio_rb32(pb);
  69. if(fps > 0){
  70. den = 1000;
  71. num = fps;
  72. }else if (fps < 0){
  73. den = 100000;
  74. num = -fps;
  75. }else{
  76. den = 10;
  77. num = 1;
  78. }
  79. w = avio_rb16(pb);
  80. h = avio_rb16(pb);
  81. c->has_sound = 0;
  82. st = av_new_stream(s, 0);
  83. if (!st)
  84. return -1;
  85. // Parse WAV data header
  86. if(avio_rl32(pb) == MKTAG('W', 'A', 'V', 'E')){
  87. uint32_t size, fsize;
  88. c->has_sound = 1;
  89. size = avio_rb32(pb);
  90. c->vidpos = avio_tell(pb) + size;
  91. avio_skip(pb, 16);
  92. fsize = avio_rl32(pb);
  93. ast = av_new_stream(s, 0);
  94. if (!ast)
  95. return -1;
  96. ret = ff_get_wav_header(pb, ast->codec, fsize);
  97. if (ret < 0)
  98. return ret;
  99. // find 'data' chunk
  100. while(avio_tell(pb) < c->vidpos && !url_feof(pb)){
  101. tag = avio_rl32(pb);
  102. fsize = avio_rl32(pb);
  103. if(tag == MKTAG('d', 'a', 't', 'a')) break;
  104. avio_skip(pb, fsize);
  105. }
  106. c->bpc = (fsize + c->frames - 1) / c->frames;
  107. if(ast->codec->block_align)
  108. c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align;
  109. c->bytes_left = fsize;
  110. c->wavpos = avio_tell(pb);
  111. avio_seek(pb, c->vidpos, SEEK_SET);
  112. }
  113. /* now we are ready: build format streams */
  114. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  115. st->codec->codec_id = CODEC_ID_DXA;
  116. st->codec->width = w;
  117. st->codec->height = h;
  118. av_reduce(&den, &num, den, num, (1UL<<31)-1);
  119. av_set_pts_info(st, 33, num, den);
  120. /* flags & 0x80 means that image is interlaced,
  121. * flags & 0x40 means that image has double height
  122. * either way set true height
  123. */
  124. if(flags & 0xC0){
  125. st->codec->height >>= 1;
  126. }
  127. c->readvid = !c->has_sound;
  128. c->vidpos = avio_tell(pb);
  129. s->start_time = 0;
  130. s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
  131. av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
  132. return 0;
  133. }
  134. static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
  135. {
  136. DXAContext *c = s->priv_data;
  137. int ret;
  138. uint32_t size;
  139. uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
  140. int pal_size = 0;
  141. if(!c->readvid && c->has_sound && c->bytes_left){
  142. c->readvid = 1;
  143. avio_seek(s->pb, c->wavpos, SEEK_SET);
  144. size = FFMIN(c->bytes_left, c->bpc);
  145. ret = av_get_packet(s->pb, pkt, size);
  146. pkt->stream_index = 1;
  147. if(ret != size)
  148. return AVERROR(EIO);
  149. c->bytes_left -= size;
  150. c->wavpos = avio_tell(s->pb);
  151. return 0;
  152. }
  153. avio_seek(s->pb, c->vidpos, SEEK_SET);
  154. while(!url_feof(s->pb) && c->frames){
  155. avio_read(s->pb, buf, 4);
  156. switch(AV_RL32(buf)){
  157. case MKTAG('N', 'U', 'L', 'L'):
  158. if(av_new_packet(pkt, 4 + pal_size) < 0)
  159. return AVERROR(ENOMEM);
  160. pkt->stream_index = 0;
  161. if(pal_size) memcpy(pkt->data, pal, pal_size);
  162. memcpy(pkt->data + pal_size, buf, 4);
  163. c->frames--;
  164. c->vidpos = avio_tell(s->pb);
  165. c->readvid = 0;
  166. return 0;
  167. case MKTAG('C', 'M', 'A', 'P'):
  168. pal_size = 768+4;
  169. memcpy(pal, buf, 4);
  170. avio_read(s->pb, pal + 4, 768);
  171. break;
  172. case MKTAG('F', 'R', 'A', 'M'):
  173. avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
  174. size = AV_RB32(buf + 5);
  175. if(size > 0xFFFFFF){
  176. av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
  177. return -1;
  178. }
  179. if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
  180. return AVERROR(ENOMEM);
  181. memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
  182. ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
  183. if(ret != size){
  184. av_free_packet(pkt);
  185. return AVERROR(EIO);
  186. }
  187. if(pal_size) memcpy(pkt->data, pal, pal_size);
  188. pkt->stream_index = 0;
  189. c->frames--;
  190. c->vidpos = avio_tell(s->pb);
  191. c->readvid = 0;
  192. return 0;
  193. default:
  194. av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
  195. return -1;
  196. }
  197. }
  198. return AVERROR(EIO);
  199. }
  200. AVInputFormat ff_dxa_demuxer = {
  201. .name = "dxa",
  202. .long_name = NULL_IF_CONFIG_SMALL("DXA"),
  203. .priv_data_size = sizeof(DXAContext),
  204. .read_probe = dxa_probe,
  205. .read_header = dxa_read_header,
  206. .read_packet = dxa_read_packet,
  207. };