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.

256 lines
7.4KB

  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_close(AVFormatContext *s)
  55. {
  56. JVDemuxContext *jv = s->priv_data;
  57. av_freep(&jv->frames);
  58. return 0;
  59. }
  60. static int read_header(AVFormatContext *s)
  61. {
  62. JVDemuxContext *jv = s->priv_data;
  63. AVIOContext *pb = s->pb;
  64. AVStream *vst, *ast;
  65. int64_t audio_pts = 0;
  66. int64_t offset;
  67. int i;
  68. avio_skip(pb, 80);
  69. ast = avformat_new_stream(s, NULL);
  70. vst = avformat_new_stream(s, NULL);
  71. if (!ast || !vst)
  72. return AVERROR(ENOMEM);
  73. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  74. vst->codec->codec_id = AV_CODEC_ID_JV;
  75. vst->codec->codec_tag = 0; /* no fourcc */
  76. vst->codec->width = avio_rl16(pb);
  77. vst->codec->height = avio_rl16(pb);
  78. vst->duration =
  79. vst->nb_frames =
  80. ast->nb_index_entries = avio_rl16(pb);
  81. avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000);
  82. avio_skip(pb, 4);
  83. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  84. ast->codec->codec_id = AV_CODEC_ID_PCM_U8;
  85. ast->codec->codec_tag = 0; /* no fourcc */
  86. ast->codec->sample_rate = avio_rl16(pb);
  87. ast->codec->channels = 1;
  88. ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
  89. avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  90. avio_skip(pb, 10);
  91. ast->index_entries = av_malloc(ast->nb_index_entries * sizeof(*ast->index_entries));
  92. if (!ast->index_entries)
  93. return AVERROR(ENOMEM);
  94. jv->frames = av_malloc(ast->nb_index_entries * sizeof(JVFrame));
  95. if (!jv->frames)
  96. return AVERROR(ENOMEM);
  97. offset = 0x68 + ast->nb_index_entries * 16;
  98. for(i = 0; i < ast->nb_index_entries; i++) {
  99. AVIndexEntry *e = ast->index_entries + i;
  100. JVFrame *jvf = jv->frames + i;
  101. /* total frame size including audio, video, palette data and padding */
  102. e->size = avio_rl32(pb);
  103. e->timestamp = i;
  104. e->pos = offset;
  105. offset += e->size;
  106. jvf->audio_size = avio_rl32(pb);
  107. jvf->video_size = avio_rl32(pb);
  108. jvf->palette_size = avio_r8(pb) ? 768 : 0;
  109. if ((jvf->video_size | jvf->audio_size) & ~0xFFFFFF ||
  110. e->size - jvf->audio_size
  111. - jvf->video_size
  112. - jvf->palette_size < 0) {
  113. if (s->error_recognition & AV_EF_EXPLODE) {
  114. read_close(s);
  115. return AVERROR_INVALIDDATA;
  116. }
  117. jvf->audio_size =
  118. jvf->video_size =
  119. jvf->palette_size = 0;
  120. }
  121. if (avio_r8(pb))
  122. av_log(s, AV_LOG_WARNING, "unsupported audio codec\n");
  123. jvf->video_type = avio_r8(pb);
  124. avio_skip(pb, 1);
  125. e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE;
  126. audio_pts += jvf->audio_size;
  127. e->flags = jvf->video_type != 1 ? AVINDEX_KEYFRAME : 0;
  128. }
  129. jv->state = JV_AUDIO;
  130. return 0;
  131. }
  132. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  133. {
  134. JVDemuxContext *jv = s->priv_data;
  135. AVIOContext *pb = s->pb;
  136. AVStream *ast = s->streams[0];
  137. while (!s->pb->eof_reached && jv->pts < ast->nb_index_entries) {
  138. const AVIndexEntry *e = ast->index_entries + jv->pts;
  139. const JVFrame *jvf = jv->frames + jv->pts;
  140. switch(jv->state) {
  141. case JV_AUDIO:
  142. jv->state++;
  143. if (jvf->audio_size ) {
  144. if (av_get_packet(s->pb, pkt, jvf->audio_size) < 0)
  145. return AVERROR(ENOMEM);
  146. pkt->stream_index = 0;
  147. pkt->pts = e->timestamp;
  148. pkt->flags |= AV_PKT_FLAG_KEY;
  149. return 0;
  150. }
  151. case JV_VIDEO:
  152. jv->state++;
  153. if (jvf->video_size || jvf->palette_size) {
  154. int size = jvf->video_size + jvf->palette_size;
  155. if (av_new_packet(pkt, size + JV_PREAMBLE_SIZE))
  156. return AVERROR(ENOMEM);
  157. AV_WL32(pkt->data, jvf->video_size);
  158. pkt->data[4] = jvf->video_type;
  159. if (avio_read(pb, pkt->data + JV_PREAMBLE_SIZE, size) < 0)
  160. return AVERROR(EIO);
  161. pkt->size = size + JV_PREAMBLE_SIZE;
  162. pkt->stream_index = 1;
  163. pkt->pts = jv->pts;
  164. if (jvf->video_type != 1)
  165. pkt->flags |= AV_PKT_FLAG_KEY;
  166. return 0;
  167. }
  168. case JV_PADDING:
  169. avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size
  170. - jvf->palette_size, 0));
  171. jv->state = JV_AUDIO;
  172. jv->pts++;
  173. }
  174. }
  175. if (s->pb->eof_reached)
  176. return AVERROR_EOF;
  177. return AVERROR(EIO);
  178. }
  179. static int read_seek(AVFormatContext *s, int stream_index,
  180. int64_t ts, int flags)
  181. {
  182. JVDemuxContext *jv = s->priv_data;
  183. AVStream *ast = s->streams[0];
  184. int i;
  185. if (flags & (AVSEEK_FLAG_BYTE|AVSEEK_FLAG_FRAME))
  186. return AVERROR(ENOSYS);
  187. switch(stream_index) {
  188. case 0:
  189. i = av_index_search_timestamp(ast, ts, flags);
  190. break;
  191. case 1:
  192. i = ts;
  193. break;
  194. default:
  195. return 0;
  196. }
  197. if (i < 0 || i >= ast->nb_index_entries)
  198. return 0;
  199. if (avio_seek(s->pb, ast->index_entries[i].pos, SEEK_SET) < 0)
  200. return -1;
  201. jv->state = JV_AUDIO;
  202. jv->pts = i;
  203. return 0;
  204. }
  205. AVInputFormat ff_jv_demuxer = {
  206. .name = "jv",
  207. .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV"),
  208. .priv_data_size = sizeof(JVDemuxContext),
  209. .read_probe = read_probe,
  210. .read_header = read_header,
  211. .read_packet = read_packet,
  212. .read_seek = read_seek,
  213. .read_close = read_close,
  214. };