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.

230 lines
6.9KB

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