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.4KB

  1. /*
  2. * Gremlin Digital Video demuxer
  3. * Copyright (c) 2017 Paul B Mahol
  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 "avio.h"
  24. #include "internal.h"
  25. typedef struct GDVContext {
  26. int is_first_video;
  27. int is_audio;
  28. int audio_size;
  29. int audio_stream_index;
  30. int video_stream_index;
  31. unsigned pal[256];
  32. } GDVContext;
  33. static int gdv_read_probe(AVProbeData *p)
  34. {
  35. if (AV_RL32(p->buf) == 0x29111994)
  36. return AVPROBE_SCORE_MAX;
  37. return 0;
  38. }
  39. static struct {
  40. uint16_t id;
  41. uint16_t width;
  42. uint16_t height;
  43. } FixedSize[] = {
  44. { 0, 320, 200},
  45. { 1, 640, 200},
  46. { 2, 320, 167},
  47. { 3, 320, 180},
  48. { 4, 320, 400},
  49. { 5, 320, 170},
  50. { 6, 160, 85},
  51. { 7, 160, 83},
  52. { 8, 160, 90},
  53. { 9, 280, 128},
  54. {10, 320, 240},
  55. {11, 320, 201},
  56. {16, 640, 400},
  57. {17, 640, 200},
  58. {18, 640, 180},
  59. {19, 640, 167},
  60. {20, 640, 170},
  61. {21, 320, 240}
  62. };
  63. static int gdv_read_header(AVFormatContext *ctx)
  64. {
  65. GDVContext *gdv = ctx->priv_data;
  66. AVIOContext *pb = ctx->pb;
  67. AVStream *vst, *ast;
  68. unsigned fps, snd_flags, vid_depth, size_id;
  69. avio_skip(pb, 4);
  70. size_id = avio_rl16(pb);
  71. vst = avformat_new_stream(ctx, 0);
  72. if (!vst)
  73. return AVERROR(ENOMEM);
  74. vst->start_time = 0;
  75. vst->duration =
  76. vst->nb_frames = avio_rl16(pb);
  77. fps = avio_rl16(pb);
  78. if (!fps)
  79. return AVERROR_INVALIDDATA;
  80. snd_flags = avio_rl16(pb);
  81. if (snd_flags & 1) {
  82. ast = avformat_new_stream(ctx, 0);
  83. if (!ast)
  84. return AVERROR(ENOMEM);
  85. ast->start_time = 0;
  86. ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  87. ast->codecpar->codec_tag = 0;
  88. ast->codecpar->sample_rate = avio_rl16(pb);
  89. ast->codecpar->channels = 1 + !!(snd_flags & 2);
  90. if (snd_flags & 8) {
  91. ast->codecpar->codec_id = AV_CODEC_ID_GREMLIN_DPCM;
  92. } else {
  93. ast->codecpar->codec_id = (snd_flags & 4) ? AV_CODEC_ID_PCM_S16LE : AV_CODEC_ID_PCM_U8;
  94. }
  95. avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
  96. gdv->audio_size = (ast->codecpar->sample_rate / fps) *
  97. ast->codecpar->channels * (1 + !!(snd_flags & 4)) / (1 + !!(snd_flags & 8));
  98. gdv->is_audio = 1;
  99. } else {
  100. avio_skip(pb, 2);
  101. }
  102. vid_depth = avio_rl16(pb);
  103. avio_skip(pb, 4);
  104. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  105. vst->codecpar->codec_id = AV_CODEC_ID_GDV;
  106. vst->codecpar->codec_tag = 0;
  107. vst->codecpar->width = avio_rl16(pb);
  108. vst->codecpar->height = avio_rl16(pb);
  109. if (vst->codecpar->width == 0 || vst->codecpar->height == 0) {
  110. int i;
  111. for (i = 0; i < FF_ARRAY_ELEMS(FixedSize) - 1; i++) {
  112. if (FixedSize[i].id == size_id)
  113. break;
  114. }
  115. vst->codecpar->width = FixedSize[i].width;
  116. vst->codecpar->height = FixedSize[i].height;
  117. }
  118. avpriv_set_pts_info(vst, 64, 1, fps);
  119. if (vid_depth & 1) {
  120. int i;
  121. for (i = 0; i < 256; i++) {
  122. unsigned r = avio_r8(pb);
  123. unsigned g = avio_r8(pb);
  124. unsigned b = avio_r8(pb);
  125. gdv->pal[i] = 0xFFU << 24 | r << 18 | g << 10 | b << 2;
  126. }
  127. }
  128. gdv->is_first_video = 1;
  129. return 0;
  130. }
  131. static int gdv_read_packet(AVFormatContext *ctx, AVPacket *pkt)
  132. {
  133. GDVContext *gdv = ctx->priv_data;
  134. AVIOContext *pb = ctx->pb;
  135. int ret;
  136. if (avio_feof(pb))
  137. return pb->error ? pb->error : AVERROR_EOF;
  138. if (gdv->audio_size && gdv->is_audio) {
  139. ret = av_get_packet(pb, pkt, gdv->audio_size);
  140. if (ret < 0)
  141. return ret;
  142. pkt->stream_index = 1;
  143. gdv->is_audio = 0;
  144. } else {
  145. uint8_t *pal;
  146. if (avio_rl16(pb) != 0x1305)
  147. return AVERROR_INVALIDDATA;
  148. ret = av_get_packet(pb, pkt, 4 + avio_rl16(pb));
  149. if (ret < 0)
  150. return ret;
  151. pkt->stream_index = 0;
  152. gdv->is_audio = 1;
  153. if (gdv->is_first_video) {
  154. pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
  155. AVPALETTE_SIZE);
  156. if (!pal) {
  157. av_packet_unref(pkt);
  158. return AVERROR(ENOMEM);
  159. }
  160. memcpy(pal, gdv->pal, AVPALETTE_SIZE);
  161. pkt->flags |= AV_PKT_FLAG_KEY;
  162. gdv->is_first_video = 0;
  163. }
  164. }
  165. return 0;
  166. }
  167. AVInputFormat ff_gdv_demuxer = {
  168. .name = "gdv",
  169. .long_name = NULL_IF_CONFIG_SMALL("Gremlin Digital Video"),
  170. .priv_data_size = sizeof(GDVContext),
  171. .read_probe = gdv_read_probe,
  172. .read_header = gdv_read_header,
  173. .read_packet = gdv_read_packet,
  174. };