jvdec.c 6.8 KB

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