jvdec.c 7.0 KB

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