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.

130 lines
3.7KB

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