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.

205 lines
5.5KB

  1. /*
  2. * Cryo Interactive Entertainment HNM4 demuxer
  3. *
  4. * Copyright (c) 2012 David Kment
  5. *
  6. * This file is part of FFmpeg .
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg ; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/intreadwrite.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #define HNM4_TAG MKTAG('H', 'N', 'M', '4')
  26. #define HNM4_SAMPLE_RATE 22050
  27. #define HNM4_FRAME_FPS 24
  28. #define HNM4_CHUNK_ID_PL 19536
  29. #define HNM4_CHUNK_ID_IZ 23113
  30. #define HNM4_CHUNK_ID_IU 21833
  31. #define HNM4_CHUNK_ID_SD 17491
  32. typedef struct Hnm4DemuxContext {
  33. uint8_t version;
  34. uint16_t width;
  35. uint16_t height;
  36. uint32_t filesize;
  37. uint32_t frames;
  38. uint32_t taboffset;
  39. uint16_t bits;
  40. uint16_t channels;
  41. uint32_t framesize;
  42. uint32_t currentframe;
  43. int64_t pts;
  44. uint32_t superchunk_remaining;
  45. AVPacket vpkt;
  46. } Hnm4DemuxContext;
  47. static int hnm_probe(AVProbeData *p)
  48. {
  49. if (p->buf_size < 4)
  50. return 0;
  51. // check for HNM4 header.
  52. // currently only HNM v4/v4A is supported
  53. if (AV_RL32(&p->buf[0]) == HNM4_TAG)
  54. return AVPROBE_SCORE_MAX;
  55. return 0;
  56. }
  57. static int hnm_read_header(AVFormatContext *s)
  58. {
  59. Hnm4DemuxContext *hnm = s->priv_data;
  60. AVIOContext *pb = s->pb;
  61. AVStream *vst;
  62. /* default context members */
  63. hnm->pts = 0;
  64. av_init_packet(&hnm->vpkt);
  65. hnm->vpkt.data = NULL;
  66. hnm->vpkt.size = 0;
  67. hnm->superchunk_remaining = 0;
  68. avio_skip(pb, 8);
  69. hnm->width = avio_rl16(pb);
  70. hnm->height = avio_rl16(pb);
  71. hnm->filesize = avio_rl32(pb);
  72. hnm->frames = avio_rl32(pb);
  73. hnm->taboffset = avio_rl32(pb);
  74. hnm->bits = avio_rl16(pb);
  75. hnm->channels = avio_rl16(pb);
  76. hnm->framesize = avio_rl32(pb);
  77. avio_skip(pb, 32);
  78. hnm->currentframe = 0;
  79. if (hnm->width < 320 || hnm->width > 640 ||
  80. hnm->height < 150 || hnm->height > 480) {
  81. av_log(s, AV_LOG_ERROR,
  82. "invalid resolution: %ux%u\n", hnm->width, hnm->height);
  83. return AVERROR_INVALIDDATA;
  84. }
  85. // TODO: find a better way to detect HNM4A
  86. if (hnm->width == 640)
  87. hnm->version = 0x4a;
  88. else
  89. hnm->version = 0x40;
  90. if (!(vst = avformat_new_stream(s, NULL)))
  91. return AVERROR(ENOMEM);
  92. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  93. vst->codec->codec_id = AV_CODEC_ID_HNM4_VIDEO;
  94. vst->codec->codec_tag = 0;
  95. vst->codec->width = hnm->width;
  96. vst->codec->height = hnm->height;
  97. vst->codec->extradata = av_mallocz(1);
  98. vst->codec->extradata_size = 1;
  99. memcpy(vst->codec->extradata, &hnm->version, 1);
  100. vst->start_time = 0;
  101. avpriv_set_pts_info(vst, 33, 1, HNM4_FRAME_FPS);
  102. return 0;
  103. }
  104. static int hnm_read_packet(AVFormatContext *s, AVPacket *pkt)
  105. {
  106. Hnm4DemuxContext *hnm = s->priv_data;
  107. AVIOContext *pb = s->pb;
  108. int ret = 0;
  109. uint32_t superchunk_size, chunk_size;
  110. uint16_t chunk_id;
  111. if (hnm->currentframe == hnm->frames || pb->eof_reached)
  112. return AVERROR_EOF;
  113. if (hnm->superchunk_remaining == 0) {
  114. /* parse next superchunk */
  115. superchunk_size = avio_rl24(pb);
  116. avio_skip(pb, 1);
  117. hnm->superchunk_remaining = superchunk_size - 4;
  118. }
  119. chunk_size = avio_rl24(pb);
  120. avio_skip(pb, 1);
  121. chunk_id = avio_rl16(pb);
  122. avio_skip(pb, 2);
  123. if (chunk_size > hnm->superchunk_remaining || !chunk_size) {
  124. av_log(s, AV_LOG_ERROR, "invalid chunk size: %u, offset: %u\n",
  125. chunk_size, (int) avio_tell(pb));
  126. avio_skip(pb, hnm->superchunk_remaining - 8);
  127. hnm->superchunk_remaining = 0;
  128. }
  129. switch (chunk_id) {
  130. case HNM4_CHUNK_ID_PL:
  131. case HNM4_CHUNK_ID_IZ:
  132. case HNM4_CHUNK_ID_IU:
  133. avio_seek(pb, -8, SEEK_CUR);
  134. ret += av_get_packet(pb, pkt, chunk_size);
  135. hnm->superchunk_remaining -= chunk_size;
  136. if (chunk_id == HNM4_CHUNK_ID_IZ || chunk_id == HNM4_CHUNK_ID_IU)
  137. hnm->currentframe++;
  138. break;
  139. case HNM4_CHUNK_ID_SD:
  140. avio_skip(pb, chunk_size - 8);
  141. hnm->superchunk_remaining -= chunk_size;
  142. break;
  143. default:
  144. av_log(s, AV_LOG_WARNING, "unknown chunk found: %d, offset: %d\n",
  145. chunk_id, (int) avio_tell(pb));
  146. avio_skip(pb, chunk_size - 8);
  147. hnm->superchunk_remaining -= chunk_size;
  148. break;
  149. }
  150. return ret;
  151. }
  152. static int hnm_read_close(AVFormatContext *s)
  153. {
  154. Hnm4DemuxContext *hnm = s->priv_data;
  155. if (hnm->vpkt.size > 0)
  156. av_free_packet(&hnm->vpkt);
  157. return 0;
  158. }
  159. AVInputFormat ff_hnm_demuxer = {
  160. .name = "hnm",
  161. .long_name = NULL_IF_CONFIG_SMALL("Cryo HNM v4"),
  162. .priv_data_size = sizeof(Hnm4DemuxContext),
  163. .read_probe = hnm_probe,
  164. .read_header = hnm_read_header,
  165. .read_packet = hnm_read_packet,
  166. .read_close = hnm_read_close,
  167. .flags = AVFMT_NO_BYTE_SEEK | AVFMT_NOGENSEARCH | AVFMT_NOBINSEARCH
  168. };