dxa.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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)
  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. if (ast->codec->sample_rate > 0)
  101. avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  102. // find 'data' chunk
  103. while(avio_tell(pb) < c->vidpos && !url_feof(pb)){
  104. tag = avio_rl32(pb);
  105. fsize = avio_rl32(pb);
  106. if(tag == MKTAG('d', 'a', 't', 'a')) break;
  107. avio_skip(pb, fsize);
  108. }
  109. c->bpc = (fsize + c->frames - 1) / c->frames;
  110. if(ast->codec->block_align)
  111. c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align;
  112. c->bytes_left = fsize;
  113. c->wavpos = avio_tell(pb);
  114. avio_seek(pb, c->vidpos, SEEK_SET);
  115. }
  116. /* now we are ready: build format streams */
  117. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  118. st->codec->codec_id = AV_CODEC_ID_DXA;
  119. st->codec->width = w;
  120. st->codec->height = h;
  121. av_reduce(&den, &num, den, num, (1UL<<31)-1);
  122. avpriv_set_pts_info(st, 33, num, den);
  123. /* flags & 0x80 means that image is interlaced,
  124. * flags & 0x40 means that image has double height
  125. * either way set true height
  126. */
  127. if(flags & 0xC0){
  128. st->codec->height >>= 1;
  129. }
  130. c->readvid = !c->has_sound;
  131. c->vidpos = avio_tell(pb);
  132. s->start_time = 0;
  133. s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
  134. av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
  135. return 0;
  136. }
  137. static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
  138. {
  139. DXAContext *c = s->priv_data;
  140. int ret;
  141. uint32_t size;
  142. uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
  143. int pal_size = 0;
  144. if(!c->readvid && c->has_sound && c->bytes_left){
  145. c->readvid = 1;
  146. avio_seek(s->pb, c->wavpos, SEEK_SET);
  147. size = FFMIN(c->bytes_left, c->bpc);
  148. ret = av_get_packet(s->pb, pkt, size);
  149. pkt->stream_index = 1;
  150. if(ret != size)
  151. return AVERROR(EIO);
  152. c->bytes_left -= size;
  153. c->wavpos = avio_tell(s->pb);
  154. return 0;
  155. }
  156. avio_seek(s->pb, c->vidpos, SEEK_SET);
  157. while(!url_feof(s->pb) && c->frames){
  158. avio_read(s->pb, buf, 4);
  159. switch(AV_RL32(buf)){
  160. case MKTAG('N', 'U', 'L', 'L'):
  161. if(av_new_packet(pkt, 4 + pal_size) < 0)
  162. return AVERROR(ENOMEM);
  163. pkt->stream_index = 0;
  164. if(pal_size) memcpy(pkt->data, pal, pal_size);
  165. memcpy(pkt->data + pal_size, buf, 4);
  166. c->frames--;
  167. c->vidpos = avio_tell(s->pb);
  168. c->readvid = 0;
  169. return 0;
  170. case MKTAG('C', 'M', 'A', 'P'):
  171. pal_size = 768+4;
  172. memcpy(pal, buf, 4);
  173. avio_read(s->pb, pal + 4, 768);
  174. break;
  175. case MKTAG('F', 'R', 'A', 'M'):
  176. avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
  177. size = AV_RB32(buf + 5);
  178. if(size > 0xFFFFFF){
  179. av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
  180. return -1;
  181. }
  182. if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
  183. return AVERROR(ENOMEM);
  184. memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
  185. ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
  186. if(ret != size){
  187. av_free_packet(pkt);
  188. return AVERROR(EIO);
  189. }
  190. if(pal_size) memcpy(pkt->data, pal, pal_size);
  191. pkt->stream_index = 0;
  192. c->frames--;
  193. c->vidpos = avio_tell(s->pb);
  194. c->readvid = 0;
  195. return 0;
  196. default:
  197. av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
  198. return -1;
  199. }
  200. }
  201. return AVERROR(EIO);
  202. }
  203. AVInputFormat ff_dxa_demuxer = {
  204. .name = "dxa",
  205. .long_name = NULL_IF_CONFIG_SMALL("DXA"),
  206. .priv_data_size = sizeof(DXAContext),
  207. .read_probe = dxa_probe,
  208. .read_header = dxa_read_header,
  209. .read_packet = dxa_read_packet,
  210. };