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