📄 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 library 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 of the License, or (at your option) any later version.
*
* This library 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 this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <limits.h>
#include <math.h> //for PI
#include "avcodec.h"
#include "dsputil.h"
#include "mpegvideo.h"
#ifdef USE_FASTMEMCPY
#include "fastmemcpy.h"
#endif
#include <assert.h>
static 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 const uint8_t ff_default_chroma_qscale_table[32]={
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
};
void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
int i;
int end;
st->scantable= src_scantable;
for(i=0; i<64; i++){
int j;
j = src_scantable[i];
st->permutated[i] = permutation[j];
#ifdef ARCH_POWERPC
st->inverse[j] = i;
#endif
}
end=-1;
for(i=0; i<64; i++){
int j;
j = st->permutated[i];
if(j>end) end=j;
st->raster_end[i]= end;
}
}
/* init common dct for both encoder and decoder */
int DCT_common_init(MpegEncContext *s)
{
#ifdef CONFIG_ENCODERS
s->dct_quantize= dct_quantize_c;
s->denoise_dct= denoise_dct_c;
#endif
#ifdef HAVE_MMX
MPV_common_init_mmx(s);
#endif
#ifdef ARCH_ALPHA
MPV_common_init_axp(s);
#endif
#ifdef HAVE_MLIB
MPV_common_init_mlib(s);
#endif
#ifdef HAVE_MMI
MPV_common_init_mmi(s);
#endif
#ifdef ARCH_ARMV4L
MPV_common_init_armv4l(s);
#endif
#ifdef ARCH_POWERPC
MPV_common_init_ppc(s);
#endif
#ifdef CONFIG_ENCODERS
s->fast_dct_quantize= s->dct_quantize;
if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
}
#endif //CONFIG_ENCODERS
/* load & permutate scantables
note: only wmv uses differnt ones
*/
if(s->alternate_scan){
ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
}else{
ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
}
ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
return 0;
}
static 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 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){
#ifndef WINCE //assert
assert(pic->data[0]);
assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
#endif
pic->type= FF_BUFFER_TYPE_SHARED;
}else{
int r;
#ifndef WINCE //assert
assert(!pic->data[0]);
#endif
r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
if(r<0 || !pic->age || !pic->type || !pic->data[0]){
//av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
return -1;
}
if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
//av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
return -1;
}
if(pic->linesize[1] != pic->linesize[2]){
//av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride missmatch)\n");
return -1;
}
s->linesize = pic->linesize[0];
s->uvlinesize= pic->linesize[1];
}
if(pic->qscale_table==NULL){
CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
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;
if(s->out_format == FMT_H264){
for(i=0; i<2; i++){
CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+2) * sizeof(int16_t))
pic->motion_val[i]= pic->motion_val_base[i]+2;
CHECKED_ALLOCZ(pic->ref_index[i] , b8_array_size * sizeof(uint8_t))
}
pic->motion_subsample_log2= 2;
}
pic->qstride= s->mb_stride;
//CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
}
//it might be nicer if the application would keep track of these but it would require a API change
memmove(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; // skiped 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 void free_picture(MpegEncContext *s, Picture *pic)
{
int i;
//DEBUGMSG(1,(L"free_picture\n"));
if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED)
{
s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
}
av_freep(&pic->mbskip_table);
av_freep(&pic->qscale_table);
av_freep(&pic->mb_type_base);
//av_freep(&pic->pan_scan);
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 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*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
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 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->dct_error_sum);
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 void MPV_common_defaults(MpegEncContext *s){
//s->y_dc_scale_table=
//s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
s->progressive_frame= 1;
s->progressive_sequence= 1;
s->picture_structure= PICT_FRAME;
s->coded_picture_number = 0;
s->picture_number = 0;
s->input_picture_number = 0;
s->picture_in_gop_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);
}
void MPV_common_end(MpegEncContext *s)
{
int i, j, k;
free_duplicate_context(s);
av_freep(&s->parse_context.buffer);
s->parse_context.buffer_size=0;
av_freep(&s->mbintra_table);
av_freep(&s->cbp_table);
av_freep(&s->mbskip_table);
av_freep(&s->prev_pict_types);
av_freep(&s->mb_index2xy);
//av_freep(&s->dct_offset);
if(s->picture)
{
for(i=0; i<MAX_PICTURE_COUNT; i++)
{
free_picture(s, &s->picture[i]);
}
}
av_freep(&s->picture);
avcodec_default_free_buffers(s->avctx);
s->context_initialized = 0;
s->last_picture_ptr= NULL;
s->next_picture_ptr= NULL;
s->current_picture_ptr= NULL;
}
/**
* 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, i, mb_array_size, mv_table_size, x, y;
dsputil_init(&s->dsp, s->avctx);
DCT_common_init(s);
s->flags= s->avctx->flags;
s->flags2= s->avctx->flags2;
s->mb_width = (s->width + 15) / 16;
s->mb_height = (s->height + 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;
s->block_wrap[0]=
s->block_wrap[1]=
s->block_wrap[2]=
s->block_wrap[3]= s->b8_stride;
s->block_wrap[4]=
s->block_wrap[5]= s->mb_stride;
y_size = s->b8_stride * (2 * s->mb_height + 1);
c_size = s->mb_stride * (s->mb_height + 1);
yc_size = y_size + 2 * c_size;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -