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.

215 lines
6.2KB

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