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.

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