dxa.c 6.6 KB

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