annotate ffmpeg/libavcodec/mpeg4video_parser.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * MPEG4 Video frame extraction
yading@10 3 * Copyright (c) 2003 Fabrice Bellard
yading@10 4 * Copyright (c) 2003 Michael Niedermayer
yading@10 5 *
yading@10 6 * This file is part of FFmpeg.
yading@10 7 *
yading@10 8 * FFmpeg is free software; you can redistribute it and/or
yading@10 9 * modify it under the terms of the GNU Lesser General Public
yading@10 10 * License as published by the Free Software Foundation; either
yading@10 11 * version 2.1 of the License, or (at your option) any later version.
yading@10 12 *
yading@10 13 * FFmpeg is distributed in the hope that it will be useful,
yading@10 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 16 * Lesser General Public License for more details.
yading@10 17 *
yading@10 18 * You should have received a copy of the GNU Lesser General Public
yading@10 19 * License along with FFmpeg; if not, write to the Free Software
yading@10 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 21 */
yading@10 22
yading@10 23 #define UNCHECKED_BITSTREAM_READER 1
yading@10 24
yading@10 25 #include "parser.h"
yading@10 26 #include "mpegvideo.h"
yading@10 27 #include "mpeg4video.h"
yading@10 28 #include "mpeg4video_parser.h"
yading@10 29
yading@10 30 struct Mp4vParseContext {
yading@10 31 ParseContext pc;
yading@10 32 struct MpegEncContext enc;
yading@10 33 int first_picture;
yading@10 34 };
yading@10 35
yading@10 36 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
yading@10 37 int vop_found, i;
yading@10 38 uint32_t state;
yading@10 39
yading@10 40 vop_found= pc->frame_start_found;
yading@10 41 state= pc->state;
yading@10 42
yading@10 43 i=0;
yading@10 44 if(!vop_found){
yading@10 45 for(i=0; i<buf_size; i++){
yading@10 46 state= (state<<8) | buf[i];
yading@10 47 if(state == 0x1B6){
yading@10 48 i++;
yading@10 49 vop_found=1;
yading@10 50 break;
yading@10 51 }
yading@10 52 }
yading@10 53 }
yading@10 54
yading@10 55 if(vop_found){
yading@10 56 /* EOF considered as end of frame */
yading@10 57 if (buf_size == 0)
yading@10 58 return 0;
yading@10 59 for(; i<buf_size; i++){
yading@10 60 state= (state<<8) | buf[i];
yading@10 61 if((state&0xFFFFFF00) == 0x100){
yading@10 62 pc->frame_start_found=0;
yading@10 63 pc->state=-1;
yading@10 64 return i-3;
yading@10 65 }
yading@10 66 }
yading@10 67 }
yading@10 68 pc->frame_start_found= vop_found;
yading@10 69 pc->state= state;
yading@10 70 return END_NOT_FOUND;
yading@10 71 }
yading@10 72
yading@10 73 /* XXX: make it use less memory */
yading@10 74 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
yading@10 75 AVCodecContext *avctx,
yading@10 76 const uint8_t *buf, int buf_size)
yading@10 77 {
yading@10 78 struct Mp4vParseContext *pc = s1->priv_data;
yading@10 79 MpegEncContext *s = &pc->enc;
yading@10 80 GetBitContext gb1, *gb = &gb1;
yading@10 81 int ret;
yading@10 82
yading@10 83 s->avctx = avctx;
yading@10 84 s->current_picture_ptr = &s->current_picture;
yading@10 85
yading@10 86 if (avctx->extradata_size && pc->first_picture){
yading@10 87 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
yading@10 88 ret = ff_mpeg4_decode_picture_header(s, gb);
yading@10 89 }
yading@10 90
yading@10 91 init_get_bits(gb, buf, 8 * buf_size);
yading@10 92 ret = ff_mpeg4_decode_picture_header(s, gb);
yading@10 93 if (s->width && (!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height)) {
yading@10 94 avcodec_set_dimensions(avctx, s->width, s->height);
yading@10 95 }
yading@10 96 if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->time_base.den>0 && ret>=0){
yading@10 97 av_assert1(s1->pts == AV_NOPTS_VALUE);
yading@10 98 av_assert1(s1->dts == AV_NOPTS_VALUE);
yading@10 99
yading@10 100 s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->time_base.den}, (AVRational){1, 1200000});
yading@10 101 }
yading@10 102
yading@10 103 s1->pict_type= s->pict_type;
yading@10 104 pc->first_picture = 0;
yading@10 105 return ret;
yading@10 106 }
yading@10 107
yading@10 108 static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
yading@10 109 {
yading@10 110 struct Mp4vParseContext *pc = s->priv_data;
yading@10 111
yading@10 112 ff_mpeg4videodec_static_init();
yading@10 113
yading@10 114 pc->first_picture = 1;
yading@10 115 pc->enc.quant_precision=5;
yading@10 116 pc->enc.slice_context_count = 1;
yading@10 117 return 0;
yading@10 118 }
yading@10 119
yading@10 120 static int mpeg4video_parse(AVCodecParserContext *s,
yading@10 121 AVCodecContext *avctx,
yading@10 122 const uint8_t **poutbuf, int *poutbuf_size,
yading@10 123 const uint8_t *buf, int buf_size)
yading@10 124 {
yading@10 125 ParseContext *pc = s->priv_data;
yading@10 126 int next;
yading@10 127
yading@10 128 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
yading@10 129 next= buf_size;
yading@10 130 }else{
yading@10 131 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
yading@10 132
yading@10 133 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
yading@10 134 *poutbuf = NULL;
yading@10 135 *poutbuf_size = 0;
yading@10 136 return buf_size;
yading@10 137 }
yading@10 138 }
yading@10 139 av_mpeg4_decode_header(s, avctx, buf, buf_size);
yading@10 140
yading@10 141 *poutbuf = buf;
yading@10 142 *poutbuf_size = buf_size;
yading@10 143 return next;
yading@10 144 }
yading@10 145
yading@10 146
yading@10 147 AVCodecParser ff_mpeg4video_parser = {
yading@10 148 .codec_ids = { AV_CODEC_ID_MPEG4 },
yading@10 149 .priv_data_size = sizeof(struct Mp4vParseContext),
yading@10 150 .parser_init = mpeg4video_parse_init,
yading@10 151 .parser_parse = mpeg4video_parse,
yading@10 152 .parser_close = ff_parse_close,
yading@10 153 .split = ff_mpeg4video_split,
yading@10 154 };