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.

133 lines
3.8KB

  1. /*
  2. * Chronomaster DFA Format Demuxer
  3. * Copyright (c) 2011 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. static int dfa_probe(AVProbeData *p)
  26. {
  27. if (p->buf_size < 4 || AV_RL32(p->buf) != MKTAG('D', 'F', 'I', 'A'))
  28. return 0;
  29. if (AV_RL32(p->buf + 16) != 0x80)
  30. return AVPROBE_SCORE_MAX / 4;
  31. return AVPROBE_SCORE_MAX;
  32. }
  33. static int dfa_read_header(AVFormatContext *s)
  34. {
  35. AVIOContext *pb = s->pb;
  36. AVStream *st;
  37. int frames;
  38. int version;
  39. uint32_t mspf;
  40. if (avio_rl32(pb) != MKTAG('D', 'F', 'I', 'A')) {
  41. av_log(s, AV_LOG_ERROR, "Invalid magic for DFA\n");
  42. return AVERROR_INVALIDDATA;
  43. }
  44. version = avio_rl16(pb);
  45. frames = avio_rl16(pb);
  46. st = avformat_new_stream(s, NULL);
  47. if (!st)
  48. return AVERROR(ENOMEM);
  49. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  50. st->codec->codec_id = AV_CODEC_ID_DFA;
  51. st->codec->width = avio_rl16(pb);
  52. st->codec->height = avio_rl16(pb);
  53. mspf = avio_rl32(pb);
  54. if (!mspf) {
  55. av_log(s, AV_LOG_WARNING, "Zero FPS reported, defaulting to 10\n");
  56. mspf = 100;
  57. }
  58. avpriv_set_pts_info(st, 24, mspf, 1000);
  59. avio_skip(pb, 128 - 16); // padding
  60. st->duration = frames;
  61. if (ff_alloc_extradata(st->codec, 2))
  62. return AVERROR(ENOMEM);
  63. AV_WL16(st->codec->extradata, version);
  64. if (version == 0x100)
  65. st->sample_aspect_ratio = (AVRational){2, 1};
  66. return 0;
  67. }
  68. static int dfa_read_packet(AVFormatContext *s, AVPacket *pkt)
  69. {
  70. AVIOContext *pb = s->pb;
  71. uint32_t frame_size;
  72. int ret, first = 1;
  73. if (pb->eof_reached)
  74. return AVERROR_EOF;
  75. if (av_get_packet(pb, pkt, 12) != 12)
  76. return AVERROR(EIO);
  77. while (!pb->eof_reached) {
  78. if (!first) {
  79. ret = av_append_packet(pb, pkt, 12);
  80. if (ret < 0) {
  81. av_free_packet(pkt);
  82. return ret;
  83. }
  84. } else
  85. first = 0;
  86. frame_size = AV_RL32(pkt->data + pkt->size - 8);
  87. if (frame_size > INT_MAX - 4) {
  88. av_log(s, AV_LOG_ERROR, "Too large chunk size: %"PRIu32"\n", frame_size);
  89. return AVERROR(EIO);
  90. }
  91. if (AV_RL32(pkt->data + pkt->size - 12) == MKTAG('E', 'O', 'F', 'R')) {
  92. if (frame_size) {
  93. av_log(s, AV_LOG_WARNING,
  94. "skipping %"PRIu32" bytes of end-of-frame marker chunk\n",
  95. frame_size);
  96. avio_skip(pb, frame_size);
  97. }
  98. return 0;
  99. }
  100. ret = av_append_packet(pb, pkt, frame_size);
  101. if (ret < 0) {
  102. av_free_packet(pkt);
  103. return ret;
  104. }
  105. }
  106. return 0;
  107. }
  108. AVInputFormat ff_dfa_demuxer = {
  109. .name = "dfa",
  110. .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
  111. .read_probe = dfa_probe,
  112. .read_header = dfa_read_header,
  113. .read_packet = dfa_read_packet,
  114. .flags = AVFMT_GENERIC_INDEX,
  115. };