mpeg4video_parser.c 4.26 KB
Newer Older
1
/*
2
 * MPEG-4 video frame extraction
3 4
 * Copyright (c) 2003 Fabrice Bellard
 * Copyright (c) 2003 Michael Niedermayer
5
 *
6
 * This file is part of Libav.
7
 *
8
 * Libav is free software; you can redistribute it and/or
9 10 11 12
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
13
 * Libav is distributed in the hope that it will be useful,
14 15 16 17 18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with Libav; if not, write to the Free Software
20 21 22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

23
#include "internal.h"
24 25
#include "parser.h"
#include "mpegvideo.h"
26
#include "mpeg4video.h"
27
#include "mpeg4video_parser.h"
28

29 30
struct Mp4vParseContext {
    ParseContext pc;
31
    Mpeg4DecContext dec_ctx;
32 33
    int first_picture;
};
34

35 36
int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
{
37 38 39
    int vop_found, i;
    uint32_t state;

40 41
    vop_found = pc->frame_start_found;
    state     = pc->state;
42

43 44 45 46 47
    i = 0;
    if (!vop_found) {
        for (i = 0; i < buf_size; i++) {
            state = (state << 8) | buf[i];
            if (state == 0x1B6) {
48
                i++;
49
                vop_found = 1;
50 51 52 53 54
                break;
            }
        }
    }

55
    if (vop_found) {
56 57 58
        /* EOF considered as end of frame */
        if (buf_size == 0)
            return 0;
59 60 61 62 63 64
        for (; i < buf_size; i++) {
            state = (state << 8) | buf[i];
            if ((state & 0xFFFFFF00) == 0x100) {
                pc->frame_start_found = 0;
                pc->state             = -1;
                return i - 3;
65 66 67
            }
        }
    }
68 69
    pc->frame_start_found = vop_found;
    pc->state             = state;
70 71 72
    return END_NOT_FOUND;
}

73
/* XXX: make it use less memory */
74 75
static int mpeg4_decode_header(AVCodecParserContext *s1, AVCodecContext *avctx,
                               const uint8_t *buf, int buf_size)
76
{
77
    struct Mp4vParseContext *pc = s1->priv_data;
78 79
    Mpeg4DecContext *dec_ctx = &pc->dec_ctx;
    MpegEncContext *s = &dec_ctx->m;
80 81 82
    GetBitContext gb1, *gb = &gb1;
    int ret;

83
    s->avctx               = avctx;
84 85
    s->current_picture_ptr = &s->current_picture;

86 87
    if (avctx->extradata_size && pc->first_picture) {
        init_get_bits(gb, avctx->extradata, avctx->extradata_size * 8);
88
        ret = ff_mpeg4_decode_picture_header(dec_ctx, gb);
89 90 91
    }

    init_get_bits(gb, buf, 8 * buf_size);
92
    ret = ff_mpeg4_decode_picture_header(dec_ctx, gb);
93 94
    if (s->width && (!avctx->width || !avctx->height ||
                     !avctx->coded_width || !avctx->coded_height)) {
95 96 97
        ret = ff_set_dimensions(avctx, s->width, s->height);
        if (ret < 0)
            return ret;
98
    }
99
    s1->pict_type     = s->pict_type;
100 101 102 103
    pc->first_picture = 0;
    return ret;
}

104
static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
105
{
106
    struct Mp4vParseContext *pc = s->priv_data;
107

108
    pc->first_picture           = 1;
109
    pc->dec_ctx.m.slice_context_count = 1;
110 111 112 113
    return 0;
}

static int mpeg4video_parse(AVCodecParserContext *s,
114 115 116
                            AVCodecContext *avctx,
                            const uint8_t **poutbuf, int *poutbuf_size,
                            const uint8_t *buf, int buf_size)
117 118 119 120
{
    ParseContext *pc = s->priv_data;
    int next;

121 122 123 124
    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
        next = buf_size;
    } else {
        next = ff_mpeg4_find_frame_end(pc, buf, buf_size);
125

126
        if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
127
            *poutbuf      = NULL;
128 129 130 131
            *poutbuf_size = 0;
            return buf_size;
        }
    }
132
    mpeg4_decode_header(s, avctx, buf, buf_size);
133

134
    *poutbuf      = buf;
135 136 137 138
    *poutbuf_size = buf_size;
    return next;
}

139
AVCodecParser ff_mpeg4video_parser = {
140
    .codec_ids      = { AV_CODEC_ID_MPEG4 },
141
    .priv_data_size = sizeof(struct Mp4vParseContext),
142 143
    .parser_init    = mpeg4video_parse_init,
    .parser_parse   = mpeg4video_parse,
144
    .parser_close   = ff_parse_close,
145
    .split          = ff_mpeg4video_split,
146
};