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.

213 lines
6.1KB

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