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.

232 lines
7.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 "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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  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 AVERROR(ENOMEM);
  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 AVERROR(ENOMEM);
  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. if ((ret = avio_read(s->pb, buf, 4)) != 4) {
  159. av_log(s, AV_LOG_ERROR, "failed reading chunk type\n");
  160. return ret < 0 ? ret : AVERROR_INVALIDDATA;
  161. }
  162. switch(AV_RL32(buf)){
  163. case MKTAG('N', 'U', 'L', 'L'):
  164. if(av_new_packet(pkt, 4 + pal_size) < 0)
  165. return AVERROR(ENOMEM);
  166. pkt->stream_index = 0;
  167. if(pal_size) memcpy(pkt->data, pal, pal_size);
  168. memcpy(pkt->data + pal_size, buf, 4);
  169. c->frames--;
  170. c->vidpos = avio_tell(s->pb);
  171. c->readvid = 0;
  172. return 0;
  173. case MKTAG('C', 'M', 'A', 'P'):
  174. pal_size = 768+4;
  175. memcpy(pal, buf, 4);
  176. avio_read(s->pb, pal + 4, 768);
  177. break;
  178. case MKTAG('F', 'R', 'A', 'M'):
  179. if ((ret = avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4)) != DXA_EXTRA_SIZE - 4) {
  180. av_log(s, AV_LOG_ERROR, "failed reading dxa_extra\n");
  181. return ret < 0 ? ret : AVERROR_INVALIDDATA;
  182. }
  183. size = AV_RB32(buf + 5);
  184. if(size > 0xFFFFFF){
  185. av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
  186. return AVERROR_INVALIDDATA;
  187. }
  188. if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
  189. return AVERROR(ENOMEM);
  190. memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
  191. ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
  192. if(ret != size){
  193. av_free_packet(pkt);
  194. return AVERROR(EIO);
  195. }
  196. if(pal_size) memcpy(pkt->data, pal, pal_size);
  197. pkt->stream_index = 0;
  198. c->frames--;
  199. c->vidpos = avio_tell(s->pb);
  200. c->readvid = 0;
  201. return 0;
  202. default:
  203. av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
  204. return AVERROR_INVALIDDATA;
  205. }
  206. }
  207. return AVERROR_EOF;
  208. }
  209. AVInputFormat ff_dxa_demuxer = {
  210. .name = "dxa",
  211. .long_name = NULL_IF_CONFIG_SMALL("DXA"),
  212. .priv_data_size = sizeof(DXAContext),
  213. .read_probe = dxa_probe,
  214. .read_header = dxa_read_header,
  215. .read_packet = dxa_read_packet,
  216. };