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.

264 lines
7.8KB

  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/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' && strlen(MAGIC) <= pd->buf_size - 4 &&
  50. !memcmp(pd->buf + 4, MAGIC, strlen(MAGIC)))
  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 *
  92. sizeof(*ast->index_entries));
  93. if (!ast->index_entries)
  94. return AVERROR(ENOMEM);
  95. jv->frames = av_malloc(ast->nb_index_entries * sizeof(JVFrame));
  96. if (!jv->frames)
  97. return AVERROR(ENOMEM);
  98. offset = 0x68 + ast->nb_index_entries * 16;
  99. for (i = 0; i < ast->nb_index_entries; i++) {
  100. AVIndexEntry *e = ast->index_entries + i;
  101. JVFrame *jvf = jv->frames + i;
  102. /* total frame size including audio, video, palette data and padding */
  103. e->size = avio_rl32(pb);
  104. e->timestamp = i;
  105. e->pos = offset;
  106. offset += e->size;
  107. jvf->audio_size = avio_rl32(pb);
  108. jvf->video_size = avio_rl32(pb);
  109. jvf->palette_size = avio_r8(pb) ? 768 : 0;
  110. if ((jvf->video_size | jvf->audio_size) & ~0xFFFFFF ||
  111. e->size - jvf->audio_size
  112. - jvf->video_size
  113. - jvf->palette_size < 0) {
  114. if (s->error_recognition & AV_EF_EXPLODE) {
  115. read_close(s);
  116. return AVERROR_INVALIDDATA;
  117. }
  118. jvf->audio_size =
  119. jvf->video_size =
  120. jvf->palette_size = 0;
  121. }
  122. if (avio_r8(pb))
  123. av_log(s, AV_LOG_WARNING, "unsupported audio codec\n");
  124. jvf->video_type = avio_r8(pb);
  125. avio_skip(pb, 1);
  126. e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE;
  127. audio_pts += jvf->audio_size;
  128. e->flags = jvf->video_type != 1 ? AVINDEX_KEYFRAME : 0;
  129. }
  130. jv->state = JV_AUDIO;
  131. return 0;
  132. }
  133. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  134. {
  135. JVDemuxContext *jv = s->priv_data;
  136. AVIOContext *pb = s->pb;
  137. AVStream *ast = s->streams[0];
  138. while (!url_feof(s->pb) && jv->pts < ast->nb_index_entries) {
  139. const AVIndexEntry *e = ast->index_entries + jv->pts;
  140. const JVFrame *jvf = jv->frames + jv->pts;
  141. switch (jv->state) {
  142. case JV_AUDIO:
  143. jv->state++;
  144. if (jvf->audio_size) {
  145. if (av_get_packet(s->pb, pkt, jvf->audio_size) < 0)
  146. return AVERROR(ENOMEM);
  147. pkt->stream_index = 0;
  148. pkt->pts = e->timestamp;
  149. pkt->flags |= AV_PKT_FLAG_KEY;
  150. return 0;
  151. }
  152. case JV_VIDEO:
  153. jv->state++;
  154. if (jvf->video_size || jvf->palette_size) {
  155. int ret;
  156. int size = jvf->video_size + jvf->palette_size;
  157. if (av_new_packet(pkt, size + JV_PREAMBLE_SIZE))
  158. return AVERROR(ENOMEM);
  159. AV_WL32(pkt->data, jvf->video_size);
  160. pkt->data[4] = jvf->video_type;
  161. ret = avio_read(pb, pkt->data + JV_PREAMBLE_SIZE, size);
  162. if (ret < 0)
  163. return ret;
  164. if (ret < size) {
  165. memset(pkt->data + JV_PREAMBLE_SIZE + ret, 0,
  166. FF_INPUT_BUFFER_PADDING_SIZE);
  167. pkt->flags |= AV_PKT_FLAG_CORRUPT;
  168. }
  169. pkt->size = ret + JV_PREAMBLE_SIZE;
  170. pkt->stream_index = 1;
  171. pkt->pts = jv->pts;
  172. if (jvf->video_type != 1)
  173. pkt->flags |= AV_PKT_FLAG_KEY;
  174. return 0;
  175. }
  176. case JV_PADDING:
  177. avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size
  178. - jvf->palette_size, 0));
  179. jv->state = JV_AUDIO;
  180. jv->pts++;
  181. }
  182. }
  183. if (s->pb->eof_reached)
  184. return AVERROR_EOF;
  185. return AVERROR(EIO);
  186. }
  187. static int read_seek(AVFormatContext *s, int stream_index,
  188. int64_t ts, int flags)
  189. {
  190. JVDemuxContext *jv = s->priv_data;
  191. AVStream *ast = s->streams[0];
  192. int i;
  193. if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
  194. return AVERROR(ENOSYS);
  195. switch (stream_index) {
  196. case 0:
  197. i = av_index_search_timestamp(ast, ts, flags);
  198. break;
  199. case 1:
  200. i = ts;
  201. break;
  202. default:
  203. return 0;
  204. }
  205. if (i < 0 || i >= ast->nb_index_entries)
  206. return 0;
  207. if (avio_seek(s->pb, ast->index_entries[i].pos, SEEK_SET) < 0)
  208. return -1;
  209. jv->state = JV_AUDIO;
  210. jv->pts = i;
  211. return 0;
  212. }
  213. AVInputFormat ff_jv_demuxer = {
  214. .name = "jv",
  215. .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV"),
  216. .priv_data_size = sizeof(JVDemuxContext),
  217. .read_probe = read_probe,
  218. .read_header = read_header,
  219. .read_packet = read_packet,
  220. .read_seek = read_seek,
  221. .read_close = read_close,
  222. };