diff libavcodec/h264.c @ 2:897f711a7157

rearrange to work with autoconf
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Tue, 25 Sep 2012 15:55:33 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libavcodec/h264.c	Tue Sep 25 15:55:33 2012 +0200
     1.3 @@ -0,0 +1,215 @@
     1.4 +#include "config.h"
     1.5 +#include "h264.h"
     1.6 +#include "h264_misc.h"
     1.7 +#include <math.h>
     1.8 +
     1.9 +H264Context *get_h264dec_context(const char *file_name, int ifile, int ofile, int width, int height, h264_options *opts){
    1.10 +    int i;
    1.11 +    const int mb_height = (height + 15) / 16;
    1.12 +    const int mb_width  = (width  + 15) / 16;
    1.13 +    const int mb_stride = ((mb_width+1)/16 + 1) *16; //align mb_stride to 16
    1.14 +
    1.15 +    ff_init_cabac_states();
    1.16 +
    1.17 +    H264Context *h= av_mallocz(sizeof(H264Context));
    1.18 +
    1.19 +    start_timer(h, TOTAL);
    1.20 +    h->file_name = file_name;
    1.21 +    h->profile = opts->profile;
    1.22 +    for (i=0; i<PROFILE_STAGES; i++)
    1.23 +        h->total_time[i]=0;
    1.24 +
    1.25 +    h->ifile=ifile;
    1.26 +    h->ofile =ofile;
    1.27 +
    1.28 +    h->verbose =opts->verbose;
    1.29 +    h->no_mbd =opts->no_mbd;
    1.30 +    h->static_3d =opts->static_3d;
    1.31 +    h->pipe_bufs = opts->pipe_bufs;
    1.32 +    h->slice_bufs = opts->slice_bufs;
    1.33 +
    1.34 +    h->ed_ppe_threads =0;
    1.35 +    if (opts->ppe_ed){
    1.36 +        h->ed_ppe_threads = (opts->threads >opts->ppe_ed)? opts->ppe_ed :opts->threads;
    1.37 +    }
    1.38 +
    1.39 +    h->threads = opts->threads - h->ed_ppe_threads;
    1.40 +    h->smt = opts->smt;
    1.41 +    if (h->smt){
    1.42 +        h->threads *= 2;
    1.43 +    }
    1.44 +
    1.45 +    h->num_frames = opts->numframes;
    1.46 +
    1.47 +    h->frame_width = width;
    1.48 +    h->frame_height = height;
    1.49 +
    1.50 +    while ((width/2) %STRIDE_ALIGN)
    1.51 +        width+=STRIDE_ALIGN;
    1.52 +    h->width = width;
    1.53 +    h->height = mb_height*16;
    1.54 +
    1.55 +    h->mb_height = mb_height;
    1.56 +    h->mb_width = mb_width;
    1.57 +    h->mb_stride = mb_stride;
    1.58 +    h->b4_stride = mb_width*4 + 1;
    1.59 +    h->b_stride = mb_width*4;
    1.60 +
    1.61 +    h->smb_width = opts->smb_size[0];
    1.62 +    h->smb_height = opts->smb_size[1] < h->smb_width ?  opts->smb_size[1]  : h->smb_width;
    1.63 +    h->smbc = getSuperMBContext(h, h->smb_width, h->smb_height);    
    1.64 +
    1.65 +    h->wave_order = opts->wave_order;
    1.66 +
    1.67 +    h->pipe_bufs = opts->pipe_bufs;
    1.68 +
    1.69 +    h->max_dpb_cnt = DPB_SIZE + opts->pipe_bufs;
    1.70 +    h->free_dpb_cnt = h->max_dpb_cnt;
    1.71 +    h->dpb = av_mallocz (h->max_dpb_cnt* sizeof (DecodedPicture));
    1.72 +    
    1.73 +
    1.74 +    h->free_sb_cnt = h->threads*opts->slice_bufs + (h->no_mbd != 0) ;  //one extra to overlap some latency of signaling/freeing slicebuffers in entropy only mode
    1.75 +    h->sb_size = h->free_sb_cnt;
    1.76 +    h->sb = av_mallocz(h->sb_size* sizeof(SliceBufferEntry));
    1.77 +
    1.78 +    h->rl_q.size = FFMAX(1, FFMIN( (h->height-3 - 512)/16, h->mb_width/2)) +1;
    1.79 +    h->rl_q.free = h->rl_q.size -1;
    1.80 +    h->rl_q.ready=0;
    1.81 +    h->rl_q.fi = h->rl_q.fo= 0;
    1.82 +    h->rl_q.queue = av_malloc(h->rl_q.size* sizeof(RingLineEntry*));
    1.83 +    for (i=0; i<h->rl_q.size; i++){
    1.84 +        if( posix_memalign((void**)&h->rl_q.queue[i],64,sizeof(RingLineEntry)))
    1.85 +            h->rl_q.queue[i]=NULL;
    1.86 +        h->rl_q.queue[i]->top = av_malloc(h->mb_width*sizeof(TopBorder));
    1.87 +    }
    1.88 +
    1.89 +    h->rl_q.queue[0]->prev_line = h->rl_q.queue[h->rl_q.size-1];
    1.90 +    for (i=1; i<h->rl_q.size; i++){
    1.91 +        h->rl_q.queue[i]->prev_line = h->rl_q.queue[i-1];
    1.92 +    }
    1.93 +
    1.94 +    if( HAVE_MMX | HAVE_ALTIVEC| HAVE_NEON ){
    1.95 +        for(i=0; i<16; i++){
    1.96 +            #define T(x) (x>>2) | ((x<<2) & 0xF)
    1.97 +            h->zigzag_scan[i] = T(zigzag_scan[i]);
    1.98 +            #undef T
    1.99 +        }
   1.100 +        for(i=0; i<64; i++){
   1.101 +            #define T(x) (x>>3) | ((x&7)<<3)
   1.102 +            h->zigzag_scan8x8[i]       = T(ff_zigzag_direct[i]);
   1.103 +            #undef T
   1.104 +        }
   1.105 +    }else{
   1.106 +        memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
   1.107 +        memcpy(h->zigzag_scan8x8, ff_zigzag_direct, 64*sizeof(uint8_t));
   1.108 +    }
   1.109 +
   1.110 +    pthread_mutex_init(&h->smb_lock, NULL);
   1.111 +    pthread_mutex_init(&h->sdl_lock, NULL);
   1.112 +    pthread_cond_init(&h->sdl_cond, NULL);
   1.113 +
   1.114 +    ///pthread initialization
   1.115 +    pthread_mutex_init(&h->ilock, NULL);
   1.116 +    pthread_cond_init(&h->icond, NULL);
   1.117 +    pthread_mutex_init(&h->slock, NULL);
   1.118 +    pthread_cond_init(&h->scond, NULL);
   1.119 +    pthread_mutex_init(&h->tlock, NULL);
   1.120 +    pthread_cond_init(&h->tcond, NULL);
   1.121 +    pthread_mutex_init(&h->tdlock, NULL);
   1.122 +    pthread_cond_init(&h->tdcond, NULL);
   1.123 +    h->start =!opts->numamap; //default dont wait for start signal
   1.124 +    h->statmbd = opts->statmbd;
   1.125 +    h->rl_side_touch= opts->numamap;
   1.126 +    h->touch_start=0;
   1.127 +    h->setaff =opts->statsched;
   1.128 +    h->init_threads=0;
   1.129 +
   1.130 +    pthread_mutex_init(&h->task_lock, NULL);
   1.131 +    pthread_cond_init(&h->task_cond, NULL);
   1.132 +    for (i=0; i<STAGES; i++){
   1.133 +        pthread_mutex_init (&h->lock[i], NULL);
   1.134 +        pthread_cond_init (&h->cond[i], NULL);
   1.135 +
   1.136 +        pthread_mutex_init (&h->sb_q[i].lock, NULL);
   1.137 +        pthread_cond_init (&h->sb_q[i].cond, NULL);
   1.138 +        h->sb_q[i].size = h->free_sb_cnt; //change to num threads later
   1.139 +        h->sb_q[i].queue = av_malloc(h->free_sb_cnt* sizeof(SliceBufferEntry*));
   1.140 +        h->sb_q[i].cnt = h->sb_q[i].fi = h->sb_q[i].fo =0;
   1.141 +    }
   1.142 +
   1.143 +#if HAVE_LIBSDL2
   1.144 +    h->sdlq.size=2;
   1.145 +    h->sdlq.ready=2;
   1.146 +    h->sdlq.queue = av_malloc(2* sizeof(SDL_Texture*));
   1.147 +    pthread_mutex_init (&h->sdlq.sdl_lock, NULL);
   1.148 +    pthread_cond_init (&h->sdlq.sdl_cond, NULL);
   1.149 +#endif
   1.150 +
   1.151 +    h->display=opts->display;
   1.152 +    h->fullscreen=opts->fullscreen;
   1.153 +
   1.154 +    return h;
   1.155 +}
   1.156 +
   1.157 +
   1.158 +void free_h264dec_context(H264Context *h) {
   1.159 +    int i;
   1.160 +
   1.161 +    for(i=0; i<h->max_dpb_cnt; i++)
   1.162 +        free_dp(&h->dpb[i]);
   1.163 +    av_free (h->dpb);
   1.164 +
   1.165 +    for(i=0; i<h->sb_size; i++){
   1.166 +        if (h->sb[i].initialized){
   1.167 +            free_sb_entry(&h->sb[i]);
   1.168 +        }
   1.169 +    }
   1.170 +    av_freep(&h->sb);
   1.171 +
   1.172 +    for (i=0; i<h->rl_q.size; i++){
   1.173 +        av_freep(&h->rl_q.queue[i]->top);
   1.174 +        av_freep(&h->rl_q.queue[i]);
   1.175 +    }
   1.176 +    av_freep(&h->rl_q.queue);
   1.177 +
   1.178 +    ///pthread cleanup
   1.179 +    pthread_mutex_destroy (&h->task_lock);
   1.180 +    pthread_cond_destroy (&h->task_cond);
   1.181 +    for (i=0; i<STAGES; i++){
   1.182 +        pthread_mutex_destroy (&h->lock[i]);
   1.183 +        pthread_cond_destroy (&h->cond[i]);
   1.184 +
   1.185 +        pthread_mutex_destroy (&h->sb_q[i].lock);
   1.186 +        pthread_cond_destroy (&h->sb_q[i].cond);
   1.187 +        av_freep( &h->sb_q[i].queue);
   1.188 +    }
   1.189 +    pthread_mutex_destroy (&h->slock);
   1.190 +    pthread_cond_destroy (&h->scond);
   1.191 +    pthread_mutex_destroy (&h->ilock);
   1.192 +    pthread_cond_destroy (&h->icond);
   1.193 +
   1.194 +    pthread_mutex_destroy(&h->smb_lock);
   1.195 +    pthread_mutex_destroy (&h->sdl_lock);
   1.196 +    pthread_cond_destroy (&h->sdl_cond);
   1.197 +#if HAVE_LIBSDL2
   1.198 +    av_free(h->sdlq.queue);
   1.199 +    pthread_mutex_destroy (&h->sdlq.sdl_lock);
   1.200 +    pthread_cond_destroy (&h->sdlq.sdl_cond);
   1.201 +#endif
   1.202 +
   1.203 +    stop_timer(h, TOTAL);
   1.204 +    if (h->threads==0){
   1.205 +        for (i=0; i<PROFILE_STAGES; i++)
   1.206 +            h->total_time[i] /= h->num_frames;
   1.207 +        double others = h->total_time[TOTAL];
   1.208 +        for (i=1; i<PROFILE_STAGES; i++)
   1.209 +            others-=h->total_time[i];
   1.210 +        if (h->profile == 1){
   1.211 +            printf("\n[FRAME %.3fms] [FRONT %.3fms] [ENTROPY %.3fms] [MBREC %.3fms] [OTHERS %.3fms]\n", h->total_time[TOTAL], h->total_time[FRONT], h->total_time[ED], h->total_time[REC], others);
   1.212 +        }else if (h->profile ==2){
   1.213 +            printf("\n[FRAME %.3fms] [FRONT %.3fms] [ENTROPY %.3fms] [PRED  %.3fms] [OTHERS %.3fms]\n", h->total_time[TOTAL], h->total_time[FRONT], h->total_time[ED],h->total_time[REC], others);
   1.214 +        }
   1.215 +    }
   1.216 +
   1.217 +    av_free(h);
   1.218 +}
   1.219 \ No newline at end of file