avformat/mpeg: demux ivtv captions
[ffmpeg.git] / libavcodec / rv34_parser.c
blob2997a4db7085a65a0512025ed6707e635f200029
1 /*
2 * RV30/40 parser
3 * Copyright (c) 2011 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
22 /**
23 * @file
24 * RV30/40 parser
27 #include "avcodec.h"
28 #include "libavutil/intreadwrite.h"
30 typedef struct RV34ParseContext {
31 int64_t key_dts;
32 int key_pts;
33 } RV34ParseContext;
35 static const int rv_to_av_frame_type[4] = {
36 AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B,
39 static int rv34_parse(AVCodecParserContext *s,
40 AVCodecContext *avctx,
41 const uint8_t **poutbuf, int *poutbuf_size,
42 const uint8_t *buf, int buf_size)
44 RV34ParseContext *pc = s->priv_data;
45 int type, pts, hdr;
47 if (buf_size < 13 + *buf * 8) {
48 *poutbuf = buf;
49 *poutbuf_size = buf_size;
50 return buf_size;
53 hdr = AV_RB32(buf + 9 + *buf * 8);
54 if (avctx->codec_id == AV_CODEC_ID_RV30) {
55 type = (hdr >> 27) & 3;
56 pts = (hdr >> 7) & 0x1FFF;
57 } else {
58 type = (hdr >> 29) & 3;
59 pts = (hdr >> 6) & 0x1FFF;
62 if (type != 3 && s->pts != AV_NOPTS_VALUE) {
63 pc->key_dts = s->pts;
64 pc->key_pts = pts;
65 } else {
66 if (type != 3)
67 s->pts = pc->key_dts + ((pts - pc->key_pts) & 0x1FFF);
68 else
69 s->pts = pc->key_dts - ((pc->key_pts - pts) & 0x1FFF);
71 s->pict_type = rv_to_av_frame_type[type];
73 *poutbuf = buf;
74 *poutbuf_size = buf_size;
75 return buf_size;
78 const AVCodecParser ff_rv34_parser = {
79 .codec_ids = { AV_CODEC_ID_RV30, AV_CODEC_ID_RV40 },
80 .priv_data_size = sizeof(RV34ParseContext),
81 .parser_parse = rv34_parse,