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.

222 lines
6.6KB

  1. /*
  2. * Bitmap Brothers JV demuxer
  3. * Copyright (c) 2005, 2011 Peter Ross <pross@xvid.org>
  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. /**
  22. * @file
  23. * Bitmap Brothers JV demuxer
  24. * @author Peter Ross <pross@xvid.org>
  25. */
  26. #include "libavutil/intreadwrite.h"
  27. #include "avformat.h"
  28. typedef struct {
  29. int audio_size; /** audio packet size (bytes) */
  30. int video_size; /** video packet size (bytes) */
  31. int palette; /** frame contains palette change */
  32. int video_type; /** per-frame video compression type */
  33. } JVFrame;
  34. typedef struct {
  35. JVFrame *frames;
  36. enum {
  37. JV_AUDIO = 0,
  38. JV_VIDEO,
  39. JV_PADDING
  40. } state;
  41. int64_t pts;
  42. } JVDemuxContext;
  43. #define MAGIC " Compression by John M Phillips Copyright (C) 1995 The Bitmap Brothers Ltd."
  44. static int read_probe(AVProbeData *pd)
  45. {
  46. if (pd->buf[0] == 'J' && pd->buf[1] == 'V' &&
  47. !memcmp(pd->buf + 4, MAGIC, FFMIN(strlen(MAGIC), pd->buf_size - 4)))
  48. return AVPROBE_SCORE_MAX;
  49. return 0;
  50. }
  51. static int read_header(AVFormatContext *s,
  52. AVFormatParameters *ap)
  53. {
  54. JVDemuxContext *jv = s->priv_data;
  55. AVIOContext *pb = s->pb;
  56. AVStream *vst, *ast;
  57. int64_t audio_pts = 0;
  58. int64_t offset;
  59. int i;
  60. avio_skip(pb, 80);
  61. ast = av_new_stream(s, 0);
  62. vst = av_new_stream(s, 1);
  63. if (!ast || !vst)
  64. return AVERROR(ENOMEM);
  65. vst->codec->codec_type = CODEC_TYPE_VIDEO;
  66. vst->codec->codec_id = CODEC_ID_JV;
  67. vst->codec->codec_tag = 0; /* no fourcc */
  68. vst->codec->width = avio_rl16(pb);
  69. vst->codec->height = avio_rl16(pb);
  70. vst->nb_frames =
  71. ast->nb_index_entries = avio_rl16(pb);
  72. av_set_pts_info(vst, 64, avio_rl16(pb), 1000);
  73. avio_skip(pb, 4);
  74. ast->codec->codec_type = CODEC_TYPE_AUDIO;
  75. ast->codec->codec_id = CODEC_ID_PCM_U8;
  76. ast->codec->codec_tag = 0; /* no fourcc */
  77. ast->codec->sample_rate = avio_rl16(pb);
  78. ast->codec->channels = 1;
  79. av_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  80. avio_skip(pb, 10);
  81. ast->index_entries = av_malloc(ast->nb_index_entries * sizeof(*ast->index_entries));
  82. if (!ast->index_entries)
  83. return AVERROR(ENOMEM);
  84. jv->frames = av_malloc(ast->nb_index_entries * sizeof(JVFrame));
  85. if (!jv->frames)
  86. return AVERROR(ENOMEM);
  87. offset = 0x68 + ast->nb_index_entries * 16;
  88. for(i = 0; i < ast->nb_index_entries; i++) {
  89. AVIndexEntry *e = ast->index_entries + i;
  90. JVFrame *jvf = jv->frames + i;
  91. /* total frame size including audio, video, palette data and padding */
  92. e->size = avio_rl32(pb);
  93. e->timestamp = i;
  94. e->pos = offset;
  95. offset += e->size;
  96. jvf->audio_size = avio_rl32(pb);
  97. jvf->video_size = avio_rl32(pb);
  98. jvf->palette = avio_r8(pb);
  99. if (avio_r8(pb))
  100. av_log(s, AV_LOG_WARNING, "unsupported audio codec\n");
  101. jvf->video_type = avio_r8(pb);
  102. avio_skip(pb, 1);
  103. e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE;
  104. audio_pts += jvf->audio_size;
  105. e->flags = jvf->video_type != 1 ? AVINDEX_KEYFRAME : 0;
  106. }
  107. jv->state = JV_AUDIO;
  108. return 0;
  109. }
  110. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  111. {
  112. JVDemuxContext *jv = s->priv_data;
  113. AVIOContext *pb = s->pb;
  114. AVStream *ast = s->streams[0];
  115. while (!url_feof(s->pb) && jv->pts < ast->nb_index_entries) {
  116. const AVIndexEntry *e = ast->index_entries + jv->pts;
  117. const JVFrame *jvf = jv->frames + jv->pts;
  118. switch(jv->state) {
  119. case JV_AUDIO:
  120. jv->state++;
  121. if (jvf->audio_size ) {
  122. if (av_get_packet(s->pb, pkt, jvf->audio_size) < 0)
  123. return AVERROR(ENOMEM);
  124. pkt->stream_index = 0;
  125. pkt->pts = e->timestamp;
  126. pkt->flags |= PKT_FLAG_KEY;
  127. return 0;
  128. }
  129. case JV_VIDEO:
  130. jv->state++;
  131. if (jvf->video_size || jvf->palette) {
  132. int size = jvf->video_size + (jvf->palette ? 768 : 0);
  133. if (av_new_packet(pkt, size + 5))
  134. return AVERROR(ENOMEM);
  135. AV_WL32(pkt->data, jvf->video_size);
  136. pkt->data[4] = jvf->video_type;
  137. if (avio_read(pb, pkt->data + 5, size) < 0)
  138. return AVERROR(EIO);
  139. pkt->size = size + 5;
  140. pkt->stream_index = 1;
  141. pkt->pts = jv->pts;
  142. if (jvf->video_type != 1)
  143. pkt->flags |= PKT_FLAG_KEY;
  144. return 0;
  145. }
  146. case JV_PADDING:
  147. avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size
  148. - (jvf->palette ? 768 : 0), 0));
  149. jv->state = JV_AUDIO;
  150. jv->pts++;
  151. }
  152. }
  153. return AVERROR(EIO);
  154. }
  155. static int read_seek(AVFormatContext *s, int stream_index,
  156. int64_t ts, int flags)
  157. {
  158. JVDemuxContext *jv = s->priv_data;
  159. AVStream *ast = s->streams[0];
  160. int i;
  161. if (flags & (AVSEEK_FLAG_BYTE|AVSEEK_FLAG_FRAME))
  162. return AVERROR_NOTSUPP;
  163. switch(stream_index) {
  164. case 0:
  165. i = av_index_search_timestamp(ast, ts, flags);
  166. break;
  167. case 1:
  168. i = ts;
  169. break;
  170. default:
  171. return 0;
  172. }
  173. if (i < 0 || i >= ast->nb_index_entries)
  174. return 0;
  175. jv->state = JV_AUDIO;
  176. jv->pts = i;
  177. avio_seek(s->pb, ast->index_entries[i].pos, SEEK_SET);
  178. return 0;
  179. }
  180. AVInputFormat ff_jv_demuxer = {
  181. .name = "jv",
  182. .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV"),
  183. .priv_data_size = sizeof(JVDemuxContext),
  184. .read_probe = read_probe,
  185. .read_header = read_header,
  186. .read_packet = read_packet,
  187. .read_seek = read_seek,
  188. };