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.

122 lines
3.5KB

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