mjpeg_parser.c 2.73 KB
Newer Older
1 2
/*
 * MJPEG parser
3
 * Copyright (c) 2000, 2001 Fabrice Bellard
4 5 6
 * Copyright (c) 2003 Alex Beregszaszi
 * Copyright (c) 2003-2004 Michael Niedermayer
 *
7
 * This file is part of Libav.
8
 *
9
 * Libav is free software; you can redistribute it and/or
10 11 12 13
 * 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.
 *
14
 * Libav is distributed in the hope that it will be useful,
15 16 17 18 19
 * 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
20
 * License along with Libav; if not, write to the Free Software
21 22 23 24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
25
 * @file
26 27 28 29 30 31 32
 * MJPEG parser.
 */

#include "parser.h"


/**
33
 * Find the end of the current frame in the bitstream.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
 * @return the position of the first byte of the next frame, or -1
 */
static int find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
    int vop_found, i;
    uint16_t state;

    vop_found= pc->frame_start_found;
    state= pc->state;

    i=0;
    if(!vop_found){
        for(i=0; i<buf_size; i++){
            state= (state<<8) | buf[i];
            if(state == 0xFFD8){
                i++;
                vop_found=1;
                break;
            }
        }
    }

    if(vop_found){
        /* EOF considered as end of frame */
        if (buf_size == 0)
            return 0;
        for(; i<buf_size; i++){
            state= (state<<8) | buf[i];
            if(state == 0xFFD8){
                pc->frame_start_found=0;
                pc->state=0;
                return i-1;
            }
        }
    }
    pc->frame_start_found= vop_found;
    pc->state= state;
    return END_NOT_FOUND;
}

static int jpeg_parse(AVCodecParserContext *s,
Baptiste Coudurier's avatar
Baptiste Coudurier committed
74 75 76
                      AVCodecContext *avctx,
                      const uint8_t **poutbuf, int *poutbuf_size,
                      const uint8_t *buf, int buf_size)
77 78 79 80
{
    ParseContext *pc = s->priv_data;
    int next;

81 82 83
    if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
        next= buf_size;
    }else{
Baptiste Coudurier's avatar
Baptiste Coudurier committed
84
        next= find_frame_end(pc, buf, buf_size);
85

Baptiste Coudurier's avatar
Baptiste Coudurier committed
86 87 88 89 90
        if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
            *poutbuf = NULL;
            *poutbuf_size = 0;
            return buf_size;
        }
91
    }
92 93 94 95 96 97 98

    *poutbuf = buf;
    *poutbuf_size = buf_size;
    return next;
}


99
AVCodecParser ff_mjpeg_parser = {
100 101 102 103
    .codec_ids      = { CODEC_ID_MJPEG },
    .priv_data_size = sizeof(ParseContext),
    .parser_parse   = jpeg_parse,
    .parser_close   = ff_parse_close,
104
};