📄 mpegvideo.c
字号:
/*
* The simplest mpeg encoder (well, it was the simplest!)
* Copyright (c) 2000,2001 Fabrice Bellard.
* Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* 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.
*
* FFmpeg is distributed in the hope that it will be useful,
* 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
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
*/
/**
* @file mpegvideo.c
* The simplest mpeg encoder (well, it was the simplest!).
*/
#include "avcodec.h"
#include "dsputil.h"
#include "mpegvideo.h"
#include <limits.h>
#ifdef USE_FASTMEMCPY
#include "libvo/fastmemcpy.h"
#endif
STATIC_FUNC void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
/* enable all paranoid tests for rounding, overflows, etc... */
//#define PARANOID
//#define DEBUG
/* for jpeg fast DCT */
#define CONST_BITS 14
static const uint16_t aanscales[64] = {
/* precomputed values scaled up by 14 bits */
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
};
static const uint8_t h263_chroma_roundtab[16] = {
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
};
STATIC_FUNC void copy_picture(Picture *dst, Picture *src){
*dst = *src;
dst->type= FF_BUFFER_TYPE_COPY;
}
/**
* allocates a Picture
* The pixels are allocated/set by calling get_buffer() if shared=0
*/
STATIC_FUNC int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
const int mb_array_size= s->mb_stride*s->mb_height;
const int b8_array_size= s->b8_stride*s->mb_height*2;
const int b4_array_size= s->b4_stride*s->mb_height*4;
int i;
if(shared){
assert(pic->data[0]);
assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
pic->type= FF_BUFFER_TYPE_SHARED;
}else{
assert(!pic->data[0]);
DP_DEC((M_TEXT("In alloc_picture\n")));
avcodec_default_get_buffer(s->avctx, (AVFrame*)pic);
s->linesize = pic->linesize[0];
s->uvlinesize= pic->linesize[1];
}
if(pic->qscale_table==NULL){
CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num * sizeof(uint32_t))
pic->mb_type= pic->mb_type_base + s->mb_stride+1;
for(i=0; i<2; i++){
CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+4) * sizeof(int16_t))
pic->motion_val[i]= pic->motion_val_base[i]+4;
CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
}
pic->motion_subsample_log2= 2;
pic->qstride= s->mb_stride;
}
//it might be nicer if the application would keep track of these but it would require a API change
MEMCPY(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
s->prev_pict_types[0]= s->pict_type;
if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
pic->age= INT_MAX; // skipped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
return 0;
fail: //for the CHECKED_ALLOCZ macro
return -1;
}
/**
* deallocates a picture
*/
STATIC_FUNC void free_picture(MpegEncContext *s, Picture *pic){
int i;
if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
}
av_freep(&pic->qscale_table);
av_freep(&pic->mb_type_base);
av_freep(&pic->dct_coeff);
pic->mb_type= NULL;
for(i=0; i<2; i++){
av_freep(&pic->motion_val_base[i]);
av_freep(&pic->ref_index[i]);
}
if(pic->type == FF_BUFFER_TYPE_SHARED){
for(i=0; i<4; i++){
pic->base[i]=
pic->data[i]= NULL;
}
pic->type= 0;
}
}
STATIC_FUNC int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
int i;
// edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*21*2); //(width + edge + align)*interlaced*MBsize*tolerance
s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*21;
//FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
CHECKED_ALLOCZ(s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t))
s->rd_scratchpad= s->me.scratchpad;
s->b_scratchpad= s->me.scratchpad;
s->obmc_scratchpad= s->me.scratchpad + 16;
CHECKED_ALLOCZ(s->blocks, 64*12*2 * sizeof(DCTELEM))
s->block= s->blocks[0];
for(i=0;i<12;i++){
s->pblocks[i] = (short *)(&s->block[i]);
}
return 0;
fail:
return -1; //free() through MPV_common_end()
}
STATIC_FUNC void free_duplicate_context(MpegEncContext *s){
if(s==NULL) return;
av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
av_freep(&s->me.scratchpad);
s->rd_scratchpad=
s->b_scratchpad=
s->obmc_scratchpad= NULL;
av_freep(&s->blocks);
s->block= NULL;
}
/**
* sets the given MpegEncContext to common defaults (same for encoding and decoding).
* the changed fields will not depend upon the prior state of the MpegEncContext.
*/
STATIC_FUNC void MPV_common_defaults(MpegEncContext *s){
s->picture_structure= PICT_FRAME;
s->picture_number = 0;
}
/**
* sets the given MpegEncContext to defaults for decoding.
* the changed fields will not depend upon the prior state of the MpegEncContext.
*/
void MPV_decode_defaults(MpegEncContext *s){
MPV_common_defaults(s);
}
/**
* init common structure for both encoder and decoder.
* this assumes that some variables like width/height are already set
*/
int MPV_common_init(MpegEncContext *s)
{
int y_size, c_size, yc_size, mb_array_size, mv_table_size, x, y;
s->mb_height = (s->height + 15) / 16;
if((s->width || s->height) && avcodec_check_dimensions(s->avctx, s->width, s->height))
return -1;
dsputil_init(&s->dsp, s->avctx);
s->flags= s->avctx->flags;
s->flags2= s->avctx->flags2;
s->mb_width = (s->width + 15) / 16;
s->mb_stride = s->mb_width + 1;
s->b8_stride = s->mb_width*2 + 1;
s->b4_stride = s->mb_width*4 + 1;
mb_array_size= s->mb_height * s->mb_stride;
mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
/* set default edge pos, will be overriden in decode_header if needed */
s->h_edge_pos= s->mb_width*16;
s->v_edge_pos= s->mb_height*16;
s->mb_num = s->mb_width * s->mb_height;
y_size = s->b8_stride * (2 * s->mb_height + 1);
c_size = s->mb_stride * (s->mb_height + 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -