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.

171 lines
4.7KB

  1. /*
  2. * THP Demuxer
  3. * Copyright (c) 2007 Marco Gerards.
  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 "avformat.h"
  22. #include "allformats.h"
  23. typedef struct ThpDemuxContext {
  24. int version;
  25. int first_frame;
  26. int first_framesz;
  27. int last_frame;
  28. int compoff;
  29. int framecnt;
  30. AVRational fps;
  31. int frame;
  32. int next_frame;
  33. int next_framesz;
  34. int video_stream_index;
  35. int compcount;
  36. unsigned char components[16];
  37. AVStream* vst;
  38. int has_audio;
  39. } ThpDemuxContext;
  40. static int thp_probe(AVProbeData *p)
  41. {
  42. /* check file header */
  43. if (p->buf_size < 4)
  44. return 0;
  45. if (AV_RL32(p->buf) == MKTAG('T', 'H', 'P', '\0'))
  46. return AVPROBE_SCORE_MAX;
  47. else
  48. return 0;
  49. }
  50. static int thp_read_header(AVFormatContext *s,
  51. AVFormatParameters *ap)
  52. {
  53. ThpDemuxContext *thp = s->priv_data;
  54. AVStream *st;
  55. ByteIOContext *pb = &s->pb;
  56. int i;
  57. /* Read the file header. */
  58. get_be32(pb); /* Skip Magic. */
  59. thp->version = get_be32(pb);
  60. get_be32(pb); /* Max buf size. */
  61. get_be32(pb); /* Max samples. */
  62. thp->fps = av_d2q(av_int2flt(get_be32(pb)), INT_MAX);
  63. thp->framecnt = get_be32(pb);
  64. thp->first_framesz = get_be32(pb);
  65. get_be32(pb); /* Data size. */
  66. thp->compoff = get_be32(pb);
  67. get_be32(pb); /* offsetDataOffset. */
  68. thp->first_frame = get_be32(pb);
  69. thp->last_frame = get_be32(pb);
  70. thp->next_framesz = thp->first_framesz;
  71. thp->next_frame = thp->first_frame;
  72. /* Read the component structure. */
  73. url_fseek (pb, thp->compoff, SEEK_SET);
  74. thp->compcount = get_be32(pb);
  75. /* Read the list of component types. */
  76. get_buffer(pb, thp->components, 16);
  77. for (i = 0; i < thp->compcount; i++) {
  78. if (thp->components[i] == 0) {
  79. if (thp->vst != 0)
  80. break;
  81. /* Video component. */
  82. st = av_new_stream(s, 0);
  83. if (!st)
  84. return AVERROR_NOMEM;
  85. /* The denominator and numerator are switched because 1/fps
  86. is required. */
  87. av_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
  88. st->codec->codec_type = CODEC_TYPE_VIDEO;
  89. st->codec->codec_id = CODEC_ID_THP;
  90. st->codec->codec_tag = 0; /* no fourcc */
  91. st->codec->width = get_be32(pb);
  92. st->codec->height = get_be32(pb);
  93. st->codec->sample_rate = av_q2d(thp->fps);
  94. thp->vst = st;
  95. thp->video_stream_index = st->index;
  96. if (thp->version == 0x11000)
  97. get_be32(pb); /* Unknown. */
  98. }
  99. else if (thp->components[i] == 1) {
  100. /* XXX: Required for audio playback. */
  101. thp->has_audio = 1;
  102. }
  103. }
  104. return 0;
  105. }
  106. static int thp_read_packet(AVFormatContext *s,
  107. AVPacket *pkt)
  108. {
  109. ThpDemuxContext *thp = s->priv_data;
  110. ByteIOContext *pb = &s->pb;
  111. int size;
  112. int ret;
  113. /* Terminate when last frame is reached. */
  114. if (thp->frame >= thp->framecnt)
  115. return AVERROR_IO;
  116. url_fseek(pb, thp->next_frame, SEEK_SET);
  117. /* Locate the next frame and read out its size. */
  118. thp->next_frame += thp->next_framesz;
  119. thp->next_framesz = get_be32(pb);
  120. get_be32(pb); /* Previous total size. */
  121. size = get_be32(pb); /* Total size of this frame. */
  122. if (thp->has_audio)
  123. get_be32(pb); /* Audio size. */
  124. ret = av_get_packet(pb, pkt, size);
  125. if (ret != size) {
  126. av_free_packet(pkt);
  127. return AVERROR_IO;
  128. }
  129. pkt->stream_index = thp->video_stream_index;
  130. thp->frame++;
  131. return 0;
  132. }
  133. AVInputFormat thp_demuxer = {
  134. "thp",
  135. "THP",
  136. sizeof(ThpDemuxContext),
  137. thp_probe,
  138. thp_read_header,
  139. thp_read_packet
  140. };