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.

229 lines
6.8KB

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