You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2432 lines
90KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file mpegvideo.c
  26. * The simplest mpeg encoder (well, it was the simplest!).
  27. */
  28. #include "avcodec.h"
  29. #include "dsputil.h"
  30. #include "mpegvideo.h"
  31. #include "mpegvideo_common.h"
  32. #include "mjpegenc.h"
  33. #include "msmpeg4.h"
  34. #include "faandct.h"
  35. #include <limits.h>
  36. //#undef NDEBUG
  37. //#include <assert.h>
  38. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  39. DCTELEM *block, int n, int qscale);
  40. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  41. DCTELEM *block, int n, int qscale);
  42. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  43. DCTELEM *block, int n, int qscale);
  44. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  45. DCTELEM *block, int n, int qscale);
  46. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  47. DCTELEM *block, int n, int qscale);
  48. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  49. DCTELEM *block, int n, int qscale);
  50. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  51. DCTELEM *block, int n, int qscale);
  52. static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
  53. #ifdef HAVE_XVMC
  54. extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
  55. extern void XVMC_field_end(MpegEncContext *s);
  56. extern void XVMC_decode_mb(MpegEncContext *s);
  57. #endif
  58. void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
  59. /* enable all paranoid tests for rounding, overflows, etc... */
  60. //#define PARANOID
  61. //#define DEBUG
  62. static const uint8_t ff_default_chroma_qscale_table[32]={
  63. // 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
  64. 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
  65. };
  66. void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
  67. int i;
  68. int end;
  69. st->scantable= src_scantable;
  70. for(i=0; i<64; i++){
  71. int j;
  72. j = src_scantable[i];
  73. st->permutated[i] = permutation[j];
  74. #ifdef ARCH_POWERPC
  75. st->inverse[j] = i;
  76. #endif
  77. }
  78. end=-1;
  79. for(i=0; i<64; i++){
  80. int j;
  81. j = st->permutated[i];
  82. if(j>end) end=j;
  83. st->raster_end[i]= end;
  84. }
  85. }
  86. const uint8_t *ff_find_start_code(const uint8_t * restrict p, const uint8_t *end, uint32_t * restrict state){
  87. int i;
  88. assert(p<=end);
  89. if(p>=end)
  90. return end;
  91. for(i=0; i<3; i++){
  92. uint32_t tmp= *state << 8;
  93. *state= tmp + *(p++);
  94. if(tmp == 0x100 || p==end)
  95. return p;
  96. }
  97. while(p<end){
  98. if (p[-1] > 1 ) p+= 3;
  99. else if(p[-2] ) p+= 2;
  100. else if(p[-3]|(p[-1]-1)) p++;
  101. else{
  102. p++;
  103. break;
  104. }
  105. }
  106. p= FFMIN(p, end)-4;
  107. *state= AV_RB32(p);
  108. return p+4;
  109. }
  110. /* init common dct for both encoder and decoder */
  111. int ff_dct_common_init(MpegEncContext *s)
  112. {
  113. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  114. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  115. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  116. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  117. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  118. if(s->flags & CODEC_FLAG_BITEXACT)
  119. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  120. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  121. #if defined(HAVE_MMX)
  122. MPV_common_init_mmx(s);
  123. #elif defined(ARCH_ALPHA)
  124. MPV_common_init_axp(s);
  125. #elif defined(HAVE_MLIB)
  126. MPV_common_init_mlib(s);
  127. #elif defined(HAVE_MMI)
  128. MPV_common_init_mmi(s);
  129. #elif defined(ARCH_ARMV4L)
  130. MPV_common_init_armv4l(s);
  131. #elif defined(HAVE_ALTIVEC)
  132. MPV_common_init_altivec(s);
  133. #elif defined(ARCH_BFIN)
  134. MPV_common_init_bfin(s);
  135. #endif
  136. /* load & permutate scantables
  137. note: only wmv uses different ones
  138. */
  139. if(s->alternate_scan){
  140. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  141. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  142. }else{
  143. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  144. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  145. }
  146. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  147. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  148. return 0;
  149. }
  150. void copy_picture(Picture *dst, Picture *src){
  151. *dst = *src;
  152. dst->type= FF_BUFFER_TYPE_COPY;
  153. }
  154. /**
  155. * allocates a Picture
  156. * The pixels are allocated/set by calling get_buffer() if shared=0
  157. */
  158. int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
  159. const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) does not sig11
  160. const int mb_array_size= s->mb_stride*s->mb_height;
  161. const int b8_array_size= s->b8_stride*s->mb_height*2;
  162. const int b4_array_size= s->b4_stride*s->mb_height*4;
  163. int i;
  164. int r= -1;
  165. if(shared){
  166. assert(pic->data[0]);
  167. assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
  168. pic->type= FF_BUFFER_TYPE_SHARED;
  169. }else{
  170. assert(!pic->data[0]);
  171. r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
  172. if(r<0 || !pic->age || !pic->type || !pic->data[0]){
  173. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
  174. return -1;
  175. }
  176. if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
  177. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
  178. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  179. return -1;
  180. }
  181. if(pic->linesize[1] != pic->linesize[2]){
  182. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride mismatch)\n");
  183. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  184. return -1;
  185. }
  186. s->linesize = pic->linesize[0];
  187. s->uvlinesize= pic->linesize[1];
  188. }
  189. if(pic->qscale_table==NULL){
  190. if (s->encoding) {
  191. CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
  192. CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
  193. CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
  194. }
  195. CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
  196. CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
  197. CHECKED_ALLOCZ(pic->mb_type_base , (big_mb_num + s->mb_stride) * sizeof(uint32_t))
  198. pic->mb_type= pic->mb_type_base + 2*s->mb_stride+1;
  199. if(s->out_format == FMT_H264){
  200. for(i=0; i<2; i++){
  201. CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+4) * sizeof(int16_t))
  202. pic->motion_val[i]= pic->motion_val_base[i]+4;
  203. CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
  204. }
  205. pic->motion_subsample_log2= 2;
  206. }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
  207. for(i=0; i<2; i++){
  208. CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+4) * sizeof(int16_t))
  209. pic->motion_val[i]= pic->motion_val_base[i]+4;
  210. CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
  211. }
  212. pic->motion_subsample_log2= 3;
  213. }
  214. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  215. CHECKED_ALLOCZ(pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6)
  216. }
  217. pic->qstride= s->mb_stride;
  218. CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
  219. }
  220. /* It might be nicer if the application would keep track of these
  221. * but it would require an API change. */
  222. memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
  223. s->prev_pict_types[0]= s->pict_type;
  224. if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
  225. pic->age= INT_MAX; // Skipped MBs in B-frames are quite rare in MPEG-1/2 and it is a bit tricky to skip them anyway.
  226. return 0;
  227. fail: //for the CHECKED_ALLOCZ macro
  228. if(r>=0)
  229. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  230. return -1;
  231. }
  232. /**
  233. * deallocates a picture
  234. */
  235. static void free_picture(MpegEncContext *s, Picture *pic){
  236. int i;
  237. if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
  238. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  239. }
  240. av_freep(&pic->mb_var);
  241. av_freep(&pic->mc_mb_var);
  242. av_freep(&pic->mb_mean);
  243. av_freep(&pic->mbskip_table);
  244. av_freep(&pic->qscale_table);
  245. av_freep(&pic->mb_type_base);
  246. av_freep(&pic->dct_coeff);
  247. av_freep(&pic->pan_scan);
  248. pic->mb_type= NULL;
  249. for(i=0; i<2; i++){
  250. av_freep(&pic->motion_val_base[i]);
  251. av_freep(&pic->ref_index[i]);
  252. }
  253. if(pic->type == FF_BUFFER_TYPE_SHARED){
  254. for(i=0; i<4; i++){
  255. pic->base[i]=
  256. pic->data[i]= NULL;
  257. }
  258. pic->type= 0;
  259. }
  260. }
  261. static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
  262. int i;
  263. // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
  264. CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*21*2); //(width + edge + align)*interlaced*MBsize*tolerance
  265. s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*21;
  266. //FIXME should be linesize instead of s->width*2 but that is not known before get_buffer()
  267. CHECKED_ALLOCZ(s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t))
  268. s->rd_scratchpad= s->me.scratchpad;
  269. s->b_scratchpad= s->me.scratchpad;
  270. s->obmc_scratchpad= s->me.scratchpad + 16;
  271. if (s->encoding) {
  272. CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
  273. CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
  274. if(s->avctx->noise_reduction){
  275. CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
  276. }
  277. }
  278. CHECKED_ALLOCZ(s->blocks, 64*12*2 * sizeof(DCTELEM))
  279. s->block= s->blocks[0];
  280. for(i=0;i<12;i++){
  281. s->pblocks[i] = (short *)(&s->block[i]);
  282. }
  283. return 0;
  284. fail:
  285. return -1; //free() through MPV_common_end()
  286. }
  287. static void free_duplicate_context(MpegEncContext *s){
  288. if(s==NULL) return;
  289. av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
  290. av_freep(&s->me.scratchpad);
  291. s->rd_scratchpad=
  292. s->b_scratchpad=
  293. s->obmc_scratchpad= NULL;
  294. av_freep(&s->dct_error_sum);
  295. av_freep(&s->me.map);
  296. av_freep(&s->me.score_map);
  297. av_freep(&s->blocks);
  298. s->block= NULL;
  299. }
  300. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src){
  301. #define COPY(a) bak->a= src->a
  302. COPY(allocated_edge_emu_buffer);
  303. COPY(edge_emu_buffer);
  304. COPY(me.scratchpad);
  305. COPY(rd_scratchpad);
  306. COPY(b_scratchpad);
  307. COPY(obmc_scratchpad);
  308. COPY(me.map);
  309. COPY(me.score_map);
  310. COPY(blocks);
  311. COPY(block);
  312. COPY(start_mb_y);
  313. COPY(end_mb_y);
  314. COPY(me.map_generation);
  315. COPY(pb);
  316. COPY(dct_error_sum);
  317. COPY(dct_count[0]);
  318. COPY(dct_count[1]);
  319. #undef COPY
  320. }
  321. void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
  322. MpegEncContext bak;
  323. int i;
  324. //FIXME copy only needed parts
  325. //START_TIMER
  326. backup_duplicate_context(&bak, dst);
  327. memcpy(dst, src, sizeof(MpegEncContext));
  328. backup_duplicate_context(dst, &bak);
  329. for(i=0;i<12;i++){
  330. dst->pblocks[i] = (short *)(&dst->block[i]);
  331. }
  332. //STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  333. }
  334. /**
  335. * sets the given MpegEncContext to common defaults (same for encoding and decoding).
  336. * the changed fields will not depend upon the prior state of the MpegEncContext.
  337. */
  338. void MPV_common_defaults(MpegEncContext *s){
  339. s->y_dc_scale_table=
  340. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  341. s->chroma_qscale_table= ff_default_chroma_qscale_table;
  342. s->progressive_frame= 1;
  343. s->progressive_sequence= 1;
  344. s->picture_structure= PICT_FRAME;
  345. s->coded_picture_number = 0;
  346. s->picture_number = 0;
  347. s->input_picture_number = 0;
  348. s->picture_in_gop_number = 0;
  349. s->f_code = 1;
  350. s->b_code = 1;
  351. }
  352. /**
  353. * sets the given MpegEncContext to defaults for decoding.
  354. * the changed fields will not depend upon the prior state of the MpegEncContext.
  355. */
  356. void MPV_decode_defaults(MpegEncContext *s){
  357. MPV_common_defaults(s);
  358. }
  359. /**
  360. * init common structure for both encoder and decoder.
  361. * this assumes that some variables like width/height are already set
  362. */
  363. int MPV_common_init(MpegEncContext *s)
  364. {
  365. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y, threads;
  366. s->mb_height = (s->height + 15) / 16;
  367. if(s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height)){
  368. av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
  369. return -1;
  370. }
  371. if((s->width || s->height) && avcodec_check_dimensions(s->avctx, s->width, s->height))
  372. return -1;
  373. dsputil_init(&s->dsp, s->avctx);
  374. ff_dct_common_init(s);
  375. s->flags= s->avctx->flags;
  376. s->flags2= s->avctx->flags2;
  377. s->mb_width = (s->width + 15) / 16;
  378. s->mb_stride = s->mb_width + 1;
  379. s->b8_stride = s->mb_width*2 + 1;
  380. s->b4_stride = s->mb_width*4 + 1;
  381. mb_array_size= s->mb_height * s->mb_stride;
  382. mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
  383. /* set chroma shifts */
  384. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
  385. &(s->chroma_y_shift) );
  386. /* set default edge pos, will be overriden in decode_header if needed */
  387. s->h_edge_pos= s->mb_width*16;
  388. s->v_edge_pos= s->mb_height*16;
  389. s->mb_num = s->mb_width * s->mb_height;
  390. s->block_wrap[0]=
  391. s->block_wrap[1]=
  392. s->block_wrap[2]=
  393. s->block_wrap[3]= s->b8_stride;
  394. s->block_wrap[4]=
  395. s->block_wrap[5]= s->mb_stride;
  396. y_size = s->b8_stride * (2 * s->mb_height + 1);
  397. c_size = s->mb_stride * (s->mb_height + 1);
  398. yc_size = y_size + 2 * c_size;
  399. /* convert fourcc to upper case */
  400. s->codec_tag= toupper( s->avctx->codec_tag &0xFF)
  401. + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
  402. + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
  403. + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
  404. s->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF)
  405. + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
  406. + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
  407. + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
  408. s->avctx->coded_frame= (AVFrame*)&s->current_picture;
  409. CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this
  410. for(y=0; y<s->mb_height; y++){
  411. for(x=0; x<s->mb_width; x++){
  412. s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
  413. }
  414. }
  415. s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
  416. if (s->encoding) {
  417. /* Allocate MV tables */
  418. CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  419. CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  420. CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  421. CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  422. CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  423. CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  424. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  425. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  426. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  427. s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  428. s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  429. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  430. if(s->msmpeg4_version){
  431. CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
  432. }
  433. CHECKED_ALLOCZ(s->avctx->stats_out, 256);
  434. /* Allocate MB type table */
  435. CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint16_t)) //needed for encoding
  436. CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
  437. CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
  438. CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
  439. CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
  440. CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
  441. CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
  442. CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
  443. if(s->avctx->noise_reduction){
  444. CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
  445. }
  446. }
  447. CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
  448. CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
  449. if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
  450. /* interlaced direct mode decoding tables */
  451. for(i=0; i<2; i++){
  452. int j, k;
  453. for(j=0; j<2; j++){
  454. for(k=0; k<2; k++){
  455. CHECKED_ALLOCZ(s->b_field_mv_table_base[i][j][k] , mv_table_size * 2 * sizeof(int16_t))
  456. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
  457. }
  458. CHECKED_ALLOCZ(s->b_field_select_table[i][j] , mb_array_size * 2 * sizeof(uint8_t))
  459. CHECKED_ALLOCZ(s->p_field_mv_table_base[i][j] , mv_table_size * 2 * sizeof(int16_t))
  460. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
  461. }
  462. CHECKED_ALLOCZ(s->p_field_select_table[i] , mb_array_size * 2 * sizeof(uint8_t))
  463. }
  464. }
  465. if (s->out_format == FMT_H263) {
  466. /* ac values */
  467. CHECKED_ALLOCZ(s->ac_val_base, yc_size * sizeof(int16_t) * 16);
  468. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  469. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  470. s->ac_val[2] = s->ac_val[1] + c_size;
  471. /* cbp values */
  472. CHECKED_ALLOCZ(s->coded_block_base, y_size);
  473. s->coded_block= s->coded_block_base + s->b8_stride + 1;
  474. /* cbp, ac_pred, pred_dir */
  475. CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
  476. CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
  477. }
  478. if (s->h263_pred || s->h263_plus || !s->encoding) {
  479. /* dc values */
  480. //MN: we need these for error resilience of intra-frames
  481. CHECKED_ALLOCZ(s->dc_val_base, yc_size * sizeof(int16_t));
  482. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  483. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  484. s->dc_val[2] = s->dc_val[1] + c_size;
  485. for(i=0;i<yc_size;i++)
  486. s->dc_val_base[i] = 1024;
  487. }
  488. /* which mb is a intra block */
  489. CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
  490. memset(s->mbintra_table, 1, mb_array_size);
  491. /* init macroblock skip table */
  492. CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
  493. //Note the +1 is for a quicker mpeg4 slice_end detection
  494. CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
  495. s->parse_context.state= -1;
  496. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  497. s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  498. s->visualization_buffer[1] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
  499. s->visualization_buffer[2] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
  500. }
  501. s->context_initialized = 1;
  502. s->thread_context[0]= s;
  503. threads = s->avctx->thread_count;
  504. for(i=1; i<threads; i++){
  505. s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
  506. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  507. }
  508. for(i=0; i<threads; i++){
  509. if(init_duplicate_context(s->thread_context[i], s) < 0)
  510. goto fail;
  511. s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count;
  512. s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
  513. }
  514. return 0;
  515. fail:
  516. MPV_common_end(s);
  517. return -1;
  518. }
  519. /* init common structure for both encoder and decoder */
  520. void MPV_common_end(MpegEncContext *s)
  521. {
  522. int i, j, k;
  523. for(i=0; i<s->avctx->thread_count; i++){
  524. free_duplicate_context(s->thread_context[i]);
  525. }
  526. for(i=1; i<s->avctx->thread_count; i++){
  527. av_freep(&s->thread_context[i]);
  528. }
  529. av_freep(&s->parse_context.buffer);
  530. s->parse_context.buffer_size=0;
  531. av_freep(&s->mb_type);
  532. av_freep(&s->p_mv_table_base);
  533. av_freep(&s->b_forw_mv_table_base);
  534. av_freep(&s->b_back_mv_table_base);
  535. av_freep(&s->b_bidir_forw_mv_table_base);
  536. av_freep(&s->b_bidir_back_mv_table_base);
  537. av_freep(&s->b_direct_mv_table_base);
  538. s->p_mv_table= NULL;
  539. s->b_forw_mv_table= NULL;
  540. s->b_back_mv_table= NULL;
  541. s->b_bidir_forw_mv_table= NULL;
  542. s->b_bidir_back_mv_table= NULL;
  543. s->b_direct_mv_table= NULL;
  544. for(i=0; i<2; i++){
  545. for(j=0; j<2; j++){
  546. for(k=0; k<2; k++){
  547. av_freep(&s->b_field_mv_table_base[i][j][k]);
  548. s->b_field_mv_table[i][j][k]=NULL;
  549. }
  550. av_freep(&s->b_field_select_table[i][j]);
  551. av_freep(&s->p_field_mv_table_base[i][j]);
  552. s->p_field_mv_table[i][j]=NULL;
  553. }
  554. av_freep(&s->p_field_select_table[i]);
  555. }
  556. av_freep(&s->dc_val_base);
  557. av_freep(&s->ac_val_base);
  558. av_freep(&s->coded_block_base);
  559. av_freep(&s->mbintra_table);
  560. av_freep(&s->cbp_table);
  561. av_freep(&s->pred_dir_table);
  562. av_freep(&s->mbskip_table);
  563. av_freep(&s->prev_pict_types);
  564. av_freep(&s->bitstream_buffer);
  565. s->allocated_bitstream_buffer_size=0;
  566. av_freep(&s->avctx->stats_out);
  567. av_freep(&s->ac_stats);
  568. av_freep(&s->error_status_table);
  569. av_freep(&s->mb_index2xy);
  570. av_freep(&s->lambda_table);
  571. av_freep(&s->q_intra_matrix);
  572. av_freep(&s->q_inter_matrix);
  573. av_freep(&s->q_intra_matrix16);
  574. av_freep(&s->q_inter_matrix16);
  575. av_freep(&s->input_picture);
  576. av_freep(&s->reordered_input_picture);
  577. av_freep(&s->dct_offset);
  578. if(s->picture){
  579. for(i=0; i<MAX_PICTURE_COUNT; i++){
  580. free_picture(s, &s->picture[i]);
  581. }
  582. }
  583. av_freep(&s->picture);
  584. s->context_initialized = 0;
  585. s->last_picture_ptr=
  586. s->next_picture_ptr=
  587. s->current_picture_ptr= NULL;
  588. s->linesize= s->uvlinesize= 0;
  589. for(i=0; i<3; i++)
  590. av_freep(&s->visualization_buffer[i]);
  591. avcodec_default_free_buffers(s->avctx);
  592. }
  593. void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3])
  594. {
  595. int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
  596. uint8_t index_run[MAX_RUN+1];
  597. int last, run, level, start, end, i;
  598. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  599. if(static_store && rl->max_level[0])
  600. return;
  601. /* compute max_level[], max_run[] and index_run[] */
  602. for(last=0;last<2;last++) {
  603. if (last == 0) {
  604. start = 0;
  605. end = rl->last;
  606. } else {
  607. start = rl->last;
  608. end = rl->n;
  609. }
  610. memset(max_level, 0, MAX_RUN + 1);
  611. memset(max_run, 0, MAX_LEVEL + 1);
  612. memset(index_run, rl->n, MAX_RUN + 1);
  613. for(i=start;i<end;i++) {
  614. run = rl->table_run[i];
  615. level = rl->table_level[i];
  616. if (index_run[run] == rl->n)
  617. index_run[run] = i;
  618. if (level > max_level[run])
  619. max_level[run] = level;
  620. if (run > max_run[level])
  621. max_run[level] = run;
  622. }
  623. if(static_store)
  624. rl->max_level[last] = static_store[last];
  625. else
  626. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  627. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  628. if(static_store)
  629. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  630. else
  631. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  632. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  633. if(static_store)
  634. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  635. else
  636. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  637. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  638. }
  639. }
  640. void init_vlc_rl(RLTable *rl, int use_static)
  641. {
  642. int i, q;
  643. /* Return if static table is already initialized */
  644. if(use_static && rl->rl_vlc[0])
  645. return;
  646. init_vlc(&rl->vlc, 9, rl->n + 1,
  647. &rl->table_vlc[0][1], 4, 2,
  648. &rl->table_vlc[0][0], 4, 2, use_static);
  649. for(q=0; q<32; q++){
  650. int qmul= q*2;
  651. int qadd= (q-1)|1;
  652. if(q==0){
  653. qmul=1;
  654. qadd=0;
  655. }
  656. if(use_static)
  657. rl->rl_vlc[q]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
  658. else
  659. rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
  660. for(i=0; i<rl->vlc.table_size; i++){
  661. int code= rl->vlc.table[i][0];
  662. int len = rl->vlc.table[i][1];
  663. int level, run;
  664. if(len==0){ // illegal code
  665. run= 66;
  666. level= MAX_LEVEL;
  667. }else if(len<0){ //more bits needed
  668. run= 0;
  669. level= code;
  670. }else{
  671. if(code==rl->n){ //esc
  672. run= 66;
  673. level= 0;
  674. }else{
  675. run= rl->table_run [code] + 1;
  676. level= rl->table_level[code] * qmul + qadd;
  677. if(code >= rl->last) run+=192;
  678. }
  679. }
  680. rl->rl_vlc[q][i].len= len;
  681. rl->rl_vlc[q][i].level= level;
  682. rl->rl_vlc[q][i].run= run;
  683. }
  684. }
  685. }
  686. /* draw the edges of width 'w' of an image of size width, height */
  687. //FIXME check that this is ok for mpeg4 interlaced
  688. static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
  689. {
  690. uint8_t *ptr, *last_line;
  691. int i;
  692. last_line = buf + (height - 1) * wrap;
  693. for(i=0;i<w;i++) {
  694. /* top and bottom */
  695. memcpy(buf - (i + 1) * wrap, buf, width);
  696. memcpy(last_line + (i + 1) * wrap, last_line, width);
  697. }
  698. /* left and right */
  699. ptr = buf;
  700. for(i=0;i<height;i++) {
  701. memset(ptr - w, ptr[0], w);
  702. memset(ptr + width, ptr[width-1], w);
  703. ptr += wrap;
  704. }
  705. /* corners */
  706. for(i=0;i<w;i++) {
  707. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  708. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  709. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  710. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  711. }
  712. }
  713. int ff_find_unused_picture(MpegEncContext *s, int shared){
  714. int i;
  715. if(shared){
  716. for(i=0; i<MAX_PICTURE_COUNT; i++){
  717. if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
  718. }
  719. }else{
  720. for(i=0; i<MAX_PICTURE_COUNT; i++){
  721. if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
  722. }
  723. for(i=0; i<MAX_PICTURE_COUNT; i++){
  724. if(s->picture[i].data[0]==NULL) return i;
  725. }
  726. }
  727. av_log(s->avctx, AV_LOG_FATAL, "Internal error, picture buffer overflow\n");
  728. /* We could return -1, but the codec would crash trying to draw into a
  729. * non-existing frame anyway. This is safer than waiting for a random crash.
  730. * Also the return of this is never useful, an encoder must only allocate
  731. * as much as allowed in the specification. This has no relationship to how
  732. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  733. * enough for such valid streams).
  734. * Plus, a decoder has to check stream validity and remove frames if too
  735. * many reference frames are around. Waiting for "OOM" is not correct at
  736. * all. Similarly, missing reference frames have to be replaced by
  737. * interpolated/MC frames, anything else is a bug in the codec ...
  738. */
  739. abort();
  740. return -1;
  741. }
  742. static void update_noise_reduction(MpegEncContext *s){
  743. int intra, i;
  744. for(intra=0; intra<2; intra++){
  745. if(s->dct_count[intra] > (1<<16)){
  746. for(i=0; i<64; i++){
  747. s->dct_error_sum[intra][i] >>=1;
  748. }
  749. s->dct_count[intra] >>= 1;
  750. }
  751. for(i=0; i<64; i++){
  752. s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
  753. }
  754. }
  755. }
  756. /**
  757. * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
  758. */
  759. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  760. {
  761. int i;
  762. AVFrame *pic;
  763. s->mb_skipped = 0;
  764. assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
  765. /* mark&release old frames */
  766. if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr->data[0]) {
  767. if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
  768. avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
  769. /* release forgotten pictures */
  770. /* if(mpeg124/h263) */
  771. if(!s->encoding){
  772. for(i=0; i<MAX_PICTURE_COUNT; i++){
  773. if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
  774. av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
  775. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  776. }
  777. }
  778. }
  779. }
  780. }
  781. alloc:
  782. if(!s->encoding){
  783. /* release non reference frames */
  784. for(i=0; i<MAX_PICTURE_COUNT; i++){
  785. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  786. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  787. }
  788. }
  789. if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
  790. pic= (AVFrame*)s->current_picture_ptr; //we allready have a unused image (maybe it was set before reading the header)
  791. else{
  792. i= ff_find_unused_picture(s, 0);
  793. pic= (AVFrame*)&s->picture[i];
  794. }
  795. pic->reference= 0;
  796. if (!s->dropable){
  797. if (s->codec_id == CODEC_ID_H264)
  798. pic->reference = s->picture_structure;
  799. else if (s->pict_type != B_TYPE)
  800. pic->reference = 3;
  801. }
  802. pic->coded_picture_number= s->coded_picture_number++;
  803. if( alloc_picture(s, (Picture*)pic, 0) < 0)
  804. return -1;
  805. s->current_picture_ptr= (Picture*)pic;
  806. s->current_picture_ptr->top_field_first= s->top_field_first; //FIXME use only the vars from current_pic
  807. s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
  808. }
  809. s->current_picture_ptr->pict_type= s->pict_type;
  810. // if(s->flags && CODEC_FLAG_QSCALE)
  811. // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
  812. s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
  813. copy_picture(&s->current_picture, s->current_picture_ptr);
  814. if (s->pict_type != B_TYPE) {
  815. s->last_picture_ptr= s->next_picture_ptr;
  816. if(!s->dropable)
  817. s->next_picture_ptr= s->current_picture_ptr;
  818. }
  819. /* av_log(s->avctx, AV_LOG_DEBUG, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n", s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  820. s->last_picture_ptr ? s->last_picture_ptr->data[0] : NULL,
  821. s->next_picture_ptr ? s->next_picture_ptr->data[0] : NULL,
  822. s->current_picture_ptr ? s->current_picture_ptr->data[0] : NULL,
  823. s->pict_type, s->dropable);*/
  824. if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
  825. if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
  826. if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL) && !s->dropable){
  827. av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
  828. assert(s->pict_type != B_TYPE); //these should have been dropped if we don't have a reference
  829. goto alloc;
  830. }
  831. assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
  832. if(s->picture_structure!=PICT_FRAME && s->out_format != FMT_H264){
  833. int i;
  834. for(i=0; i<4; i++){
  835. if(s->picture_structure == PICT_BOTTOM_FIELD){
  836. s->current_picture.data[i] += s->current_picture.linesize[i];
  837. }
  838. s->current_picture.linesize[i] *= 2;
  839. s->last_picture.linesize[i] *=2;
  840. s->next_picture.linesize[i] *=2;
  841. }
  842. }
  843. s->hurry_up= s->avctx->hurry_up;
  844. s->error_resilience= avctx->error_resilience;
  845. /* set dequantizer, we can't do it during init as it might change for mpeg4
  846. and we can't do it in the header decode as init is not called for mpeg4 there yet */
  847. if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
  848. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  849. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  850. }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  851. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  852. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  853. }else{
  854. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  855. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  856. }
  857. if(s->dct_error_sum){
  858. assert(s->avctx->noise_reduction && s->encoding);
  859. update_noise_reduction(s);
  860. }
  861. #ifdef HAVE_XVMC
  862. if(s->avctx->xvmc_acceleration)
  863. return XVMC_field_start(s, avctx);
  864. #endif
  865. return 0;
  866. }
  867. /* generic function for encode/decode called after a frame has been coded/decoded */
  868. void MPV_frame_end(MpegEncContext *s)
  869. {
  870. int i;
  871. /* draw edge for correct motion prediction if outside */
  872. #ifdef HAVE_XVMC
  873. //just to make sure that all data is rendered.
  874. if(s->avctx->xvmc_acceleration){
  875. XVMC_field_end(s);
  876. }else
  877. #endif
  878. if(s->unrestricted_mv && s->current_picture.reference && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  879. draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
  880. draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  881. draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  882. }
  883. emms_c();
  884. s->last_pict_type = s->pict_type;
  885. s->last_lambda_for[s->pict_type]= s->current_picture_ptr->quality;
  886. if(s->pict_type!=B_TYPE){
  887. s->last_non_b_pict_type= s->pict_type;
  888. }
  889. #if 0
  890. /* copy back current_picture variables */
  891. for(i=0; i<MAX_PICTURE_COUNT; i++){
  892. if(s->picture[i].data[0] == s->current_picture.data[0]){
  893. s->picture[i]= s->current_picture;
  894. break;
  895. }
  896. }
  897. assert(i<MAX_PICTURE_COUNT);
  898. #endif
  899. if(s->encoding){
  900. /* release non-reference frames */
  901. for(i=0; i<MAX_PICTURE_COUNT; i++){
  902. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  903. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  904. }
  905. }
  906. }
  907. // clear copies, to avoid confusion
  908. #if 0
  909. memset(&s->last_picture, 0, sizeof(Picture));
  910. memset(&s->next_picture, 0, sizeof(Picture));
  911. memset(&s->current_picture, 0, sizeof(Picture));
  912. #endif
  913. s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
  914. }
  915. /**
  916. * draws an line from (ex, ey) -> (sx, sy).
  917. * @param w width of the image
  918. * @param h height of the image
  919. * @param stride stride/linesize of the image
  920. * @param color color of the arrow
  921. */
  922. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  923. int x, y, fr, f;
  924. sx= av_clip(sx, 0, w-1);
  925. sy= av_clip(sy, 0, h-1);
  926. ex= av_clip(ex, 0, w-1);
  927. ey= av_clip(ey, 0, h-1);
  928. buf[sy*stride + sx]+= color;
  929. if(FFABS(ex - sx) > FFABS(ey - sy)){
  930. if(sx > ex){
  931. FFSWAP(int, sx, ex);
  932. FFSWAP(int, sy, ey);
  933. }
  934. buf+= sx + sy*stride;
  935. ex-= sx;
  936. f= ((ey-sy)<<16)/ex;
  937. for(x= 0; x <= ex; x++){
  938. y = (x*f)>>16;
  939. fr= (x*f)&0xFFFF;
  940. buf[ y *stride + x]+= (color*(0x10000-fr))>>16;
  941. buf[(y+1)*stride + x]+= (color* fr )>>16;
  942. }
  943. }else{
  944. if(sy > ey){
  945. FFSWAP(int, sx, ex);
  946. FFSWAP(int, sy, ey);
  947. }
  948. buf+= sx + sy*stride;
  949. ey-= sy;
  950. if(ey) f= ((ex-sx)<<16)/ey;
  951. else f= 0;
  952. for(y= 0; y <= ey; y++){
  953. x = (y*f)>>16;
  954. fr= (y*f)&0xFFFF;
  955. buf[y*stride + x ]+= (color*(0x10000-fr))>>16;;
  956. buf[y*stride + x+1]+= (color* fr )>>16;;
  957. }
  958. }
  959. }
  960. /**
  961. * draws an arrow from (ex, ey) -> (sx, sy).
  962. * @param w width of the image
  963. * @param h height of the image
  964. * @param stride stride/linesize of the image
  965. * @param color color of the arrow
  966. */
  967. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  968. int dx,dy;
  969. sx= av_clip(sx, -100, w+100);
  970. sy= av_clip(sy, -100, h+100);
  971. ex= av_clip(ex, -100, w+100);
  972. ey= av_clip(ey, -100, h+100);
  973. dx= ex - sx;
  974. dy= ey - sy;
  975. if(dx*dx + dy*dy > 3*3){
  976. int rx= dx + dy;
  977. int ry= -dx + dy;
  978. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  979. //FIXME subpixel accuracy
  980. rx= ROUNDED_DIV(rx*3<<4, length);
  981. ry= ROUNDED_DIV(ry*3<<4, length);
  982. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  983. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  984. }
  985. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  986. }
  987. /**
  988. * prints debuging info for the given picture.
  989. */
  990. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
  991. if(!pict || !pict->mb_type) return;
  992. if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
  993. int x,y;
  994. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  995. switch (pict->pict_type) {
  996. case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
  997. case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
  998. case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
  999. case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
  1000. case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
  1001. case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;
  1002. }
  1003. for(y=0; y<s->mb_height; y++){
  1004. for(x=0; x<s->mb_width; x++){
  1005. if(s->avctx->debug&FF_DEBUG_SKIP){
  1006. int count= s->mbskip_table[x + y*s->mb_stride];
  1007. if(count>9) count=9;
  1008. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1009. }
  1010. if(s->avctx->debug&FF_DEBUG_QP){
  1011. av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
  1012. }
  1013. if(s->avctx->debug&FF_DEBUG_MB_TYPE){
  1014. int mb_type= pict->mb_type[x + y*s->mb_stride];
  1015. //Type & MV direction
  1016. if(IS_PCM(mb_type))
  1017. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1018. else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1019. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1020. else if(IS_INTRA4x4(mb_type))
  1021. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1022. else if(IS_INTRA16x16(mb_type))
  1023. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1024. else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1025. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1026. else if(IS_DIRECT(mb_type))
  1027. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1028. else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
  1029. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1030. else if(IS_GMC(mb_type))
  1031. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1032. else if(IS_SKIP(mb_type))
  1033. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1034. else if(!USES_LIST(mb_type, 1))
  1035. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1036. else if(!USES_LIST(mb_type, 0))
  1037. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1038. else{
  1039. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1040. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1041. }
  1042. //segmentation
  1043. if(IS_8X8(mb_type))
  1044. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1045. else if(IS_16X8(mb_type))
  1046. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1047. else if(IS_8X16(mb_type))
  1048. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1049. else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
  1050. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1051. else
  1052. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1053. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
  1054. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1055. else
  1056. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1057. }
  1058. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1059. }
  1060. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1061. }
  1062. }
  1063. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  1064. const int shift= 1 + s->quarter_sample;
  1065. int mb_y;
  1066. uint8_t *ptr;
  1067. int i;
  1068. int h_chroma_shift, v_chroma_shift;
  1069. const int width = s->avctx->width;
  1070. const int height= s->avctx->height;
  1071. const int mv_sample_log2= 4 - pict->motion_subsample_log2;
  1072. const int mv_stride= (s->mb_width << mv_sample_log2) + (s->codec_id == CODEC_ID_H264 ? 0 : 1);
  1073. s->low_delay=0; //needed to see the vectors without trashing the buffers
  1074. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1075. for(i=0; i<3; i++){
  1076. memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*height:pict->linesize[i]*height >> v_chroma_shift);
  1077. pict->data[i]= s->visualization_buffer[i];
  1078. }
  1079. pict->type= FF_BUFFER_TYPE_COPY;
  1080. ptr= pict->data[0];
  1081. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  1082. int mb_x;
  1083. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  1084. const int mb_index= mb_x + mb_y*s->mb_stride;
  1085. if((s->avctx->debug_mv) && pict->motion_val){
  1086. int type;
  1087. for(type=0; type<3; type++){
  1088. int direction = 0;
  1089. switch (type) {
  1090. case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=FF_P_TYPE))
  1091. continue;
  1092. direction = 0;
  1093. break;
  1094. case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=FF_B_TYPE))
  1095. continue;
  1096. direction = 0;
  1097. break;
  1098. case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=FF_B_TYPE))
  1099. continue;
  1100. direction = 1;
  1101. break;
  1102. }
  1103. if(!USES_LIST(pict->mb_type[mb_index], direction))
  1104. continue;
  1105. if(IS_8X8(pict->mb_type[mb_index])){
  1106. int i;
  1107. for(i=0; i<4; i++){
  1108. int sx= mb_x*16 + 4 + 8*(i&1);
  1109. int sy= mb_y*16 + 4 + 8*(i>>1);
  1110. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1111. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1112. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1113. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1114. }
  1115. }else if(IS_16X8(pict->mb_type[mb_index])){
  1116. int i;
  1117. for(i=0; i<2; i++){
  1118. int sx=mb_x*16 + 8;
  1119. int sy=mb_y*16 + 4 + 8*i;
  1120. int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << (mv_sample_log2-1);
  1121. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1122. int my=(pict->motion_val[direction][xy][1]>>shift);
  1123. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1124. my*=2;
  1125. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1126. }
  1127. }else if(IS_8X16(pict->mb_type[mb_index])){
  1128. int i;
  1129. for(i=0; i<2; i++){
  1130. int sx=mb_x*16 + 4 + 8*i;
  1131. int sy=mb_y*16 + 8;
  1132. int xy= (mb_x*2 + i + mb_y*2*mv_stride) << (mv_sample_log2-1);
  1133. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1134. int my=(pict->motion_val[direction][xy][1]>>shift);
  1135. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1136. my*=2;
  1137. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1138. }
  1139. }else{
  1140. int sx= mb_x*16 + 8;
  1141. int sy= mb_y*16 + 8;
  1142. int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2;
  1143. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1144. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1145. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1146. }
  1147. }
  1148. }
  1149. if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
  1150. uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
  1151. int y;
  1152. for(y=0; y<8; y++){
  1153. *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= c;
  1154. *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= c;
  1155. }
  1156. }
  1157. if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
  1158. int mb_type= pict->mb_type[mb_index];
  1159. uint64_t u,v;
  1160. int y;
  1161. #define COLOR(theta, r)\
  1162. u= (int)(128 + r*cos(theta*3.141592/180));\
  1163. v= (int)(128 + r*sin(theta*3.141592/180));
  1164. u=v=128;
  1165. if(IS_PCM(mb_type)){
  1166. COLOR(120,48)
  1167. }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
  1168. COLOR(30,48)
  1169. }else if(IS_INTRA4x4(mb_type)){
  1170. COLOR(90,48)
  1171. }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
  1172. // COLOR(120,48)
  1173. }else if(IS_DIRECT(mb_type)){
  1174. COLOR(150,48)
  1175. }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
  1176. COLOR(170,48)
  1177. }else if(IS_GMC(mb_type)){
  1178. COLOR(190,48)
  1179. }else if(IS_SKIP(mb_type)){
  1180. // COLOR(180,48)
  1181. }else if(!USES_LIST(mb_type, 1)){
  1182. COLOR(240,48)
  1183. }else if(!USES_LIST(mb_type, 0)){
  1184. COLOR(0,48)
  1185. }else{
  1186. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1187. COLOR(300,48)
  1188. }
  1189. u*= 0x0101010101010101ULL;
  1190. v*= 0x0101010101010101ULL;
  1191. for(y=0; y<8; y++){
  1192. *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= u;
  1193. *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= v;
  1194. }
  1195. //segmentation
  1196. if(IS_8X8(mb_type) || IS_16X8(mb_type)){
  1197. *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1198. *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1199. }
  1200. if(IS_8X8(mb_type) || IS_8X16(mb_type)){
  1201. for(y=0; y<16; y++)
  1202. pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
  1203. }
  1204. if(IS_8X8(mb_type) && mv_sample_log2 >= 2){
  1205. int dm= 1 << (mv_sample_log2-2);
  1206. for(i=0; i<4; i++){
  1207. int sx= mb_x*16 + 8*(i&1);
  1208. int sy= mb_y*16 + 8*(i>>1);
  1209. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1210. //FIXME bidir
  1211. int32_t *mv = (int32_t*)&pict->motion_val[0][xy];
  1212. if(mv[0] != mv[dm] || mv[dm*mv_stride] != mv[dm*(mv_stride+1)])
  1213. for(y=0; y<8; y++)
  1214. pict->data[0][sx + 4 + (sy + y)*pict->linesize[0]]^= 0x80;
  1215. if(mv[0] != mv[dm*mv_stride] || mv[dm] != mv[dm*(mv_stride+1)])
  1216. *(uint64_t*)(pict->data[0] + sx + (sy + 4)*pict->linesize[0])^= 0x8080808080808080ULL;
  1217. }
  1218. }
  1219. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
  1220. // hmm
  1221. }
  1222. }
  1223. s->mbskip_table[mb_index]=0;
  1224. }
  1225. }
  1226. }
  1227. }
  1228. /**
  1229. * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples.
  1230. * @param buf destination buffer
  1231. * @param src source buffer
  1232. * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers
  1233. * @param block_w width of block
  1234. * @param block_h height of block
  1235. * @param src_x x coordinate of the top left sample of the block in the source buffer
  1236. * @param src_y y coordinate of the top left sample of the block in the source buffer
  1237. * @param w width of the source buffer
  1238. * @param h height of the source buffer
  1239. */
  1240. void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
  1241. int src_x, int src_y, int w, int h){
  1242. int x, y;
  1243. int start_y, start_x, end_y, end_x;
  1244. if(src_y>= h){
  1245. src+= (h-1-src_y)*linesize;
  1246. src_y=h-1;
  1247. }else if(src_y<=-block_h){
  1248. src+= (1-block_h-src_y)*linesize;
  1249. src_y=1-block_h;
  1250. }
  1251. if(src_x>= w){
  1252. src+= (w-1-src_x);
  1253. src_x=w-1;
  1254. }else if(src_x<=-block_w){
  1255. src+= (1-block_w-src_x);
  1256. src_x=1-block_w;
  1257. }
  1258. start_y= FFMAX(0, -src_y);
  1259. start_x= FFMAX(0, -src_x);
  1260. end_y= FFMIN(block_h, h-src_y);
  1261. end_x= FFMIN(block_w, w-src_x);
  1262. // copy existing part
  1263. for(y=start_y; y<end_y; y++){
  1264. for(x=start_x; x<end_x; x++){
  1265. buf[x + y*linesize]= src[x + y*linesize];
  1266. }
  1267. }
  1268. //top
  1269. for(y=0; y<start_y; y++){
  1270. for(x=start_x; x<end_x; x++){
  1271. buf[x + y*linesize]= buf[x + start_y*linesize];
  1272. }
  1273. }
  1274. //bottom
  1275. for(y=end_y; y<block_h; y++){
  1276. for(x=start_x; x<end_x; x++){
  1277. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  1278. }
  1279. }
  1280. for(y=0; y<block_h; y++){
  1281. //left
  1282. for(x=0; x<start_x; x++){
  1283. buf[x + y*linesize]= buf[start_x + y*linesize];
  1284. }
  1285. //right
  1286. for(x=end_x; x<block_w; x++){
  1287. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  1288. }
  1289. }
  1290. }
  1291. static inline int hpel_motion_lowres(MpegEncContext *s,
  1292. uint8_t *dest, uint8_t *src,
  1293. int field_based, int field_select,
  1294. int src_x, int src_y,
  1295. int width, int height, int stride,
  1296. int h_edge_pos, int v_edge_pos,
  1297. int w, int h, h264_chroma_mc_func *pix_op,
  1298. int motion_x, int motion_y)
  1299. {
  1300. const int lowres= s->avctx->lowres;
  1301. const int s_mask= (2<<lowres)-1;
  1302. int emu=0;
  1303. int sx, sy;
  1304. if(s->quarter_sample){
  1305. motion_x/=2;
  1306. motion_y/=2;
  1307. }
  1308. sx= motion_x & s_mask;
  1309. sy= motion_y & s_mask;
  1310. src_x += motion_x >> (lowres+1);
  1311. src_y += motion_y >> (lowres+1);
  1312. src += src_y * stride + src_x;
  1313. if( (unsigned)src_x > h_edge_pos - (!!sx) - w
  1314. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1315. ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  1316. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1317. src= s->edge_emu_buffer;
  1318. emu=1;
  1319. }
  1320. sx <<= 2 - lowres;
  1321. sy <<= 2 - lowres;
  1322. if(field_select)
  1323. src += s->linesize;
  1324. pix_op[lowres](dest, src, stride, h, sx, sy);
  1325. return emu;
  1326. }
  1327. /* apply one mpeg motion vector to the three components */
  1328. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1329. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1330. int field_based, int bottom_field, int field_select,
  1331. uint8_t **ref_picture, h264_chroma_mc_func *pix_op,
  1332. int motion_x, int motion_y, int h)
  1333. {
  1334. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1335. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy, uvsx, uvsy;
  1336. const int lowres= s->avctx->lowres;
  1337. const int block_s= 8>>lowres;
  1338. const int s_mask= (2<<lowres)-1;
  1339. const int h_edge_pos = s->h_edge_pos >> lowres;
  1340. const int v_edge_pos = s->v_edge_pos >> lowres;
  1341. linesize = s->current_picture.linesize[0] << field_based;
  1342. uvlinesize = s->current_picture.linesize[1] << field_based;
  1343. if(s->quarter_sample){ //FIXME obviously not perfect but qpel wont work in lowres anyway
  1344. motion_x/=2;
  1345. motion_y/=2;
  1346. }
  1347. if(field_based){
  1348. motion_y += (bottom_field - field_select)*((1<<lowres)-1);
  1349. }
  1350. sx= motion_x & s_mask;
  1351. sy= motion_y & s_mask;
  1352. src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1));
  1353. src_y =(s->mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1));
  1354. if (s->out_format == FMT_H263) {
  1355. uvsx = ((motion_x>>1) & s_mask) | (sx&1);
  1356. uvsy = ((motion_y>>1) & s_mask) | (sy&1);
  1357. uvsrc_x = src_x>>1;
  1358. uvsrc_y = src_y>>1;
  1359. }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  1360. mx = motion_x / 4;
  1361. my = motion_y / 4;
  1362. uvsx = (2*mx) & s_mask;
  1363. uvsy = (2*my) & s_mask;
  1364. uvsrc_x = s->mb_x*block_s + (mx >> lowres);
  1365. uvsrc_y = s->mb_y*block_s + (my >> lowres);
  1366. } else {
  1367. mx = motion_x / 2;
  1368. my = motion_y / 2;
  1369. uvsx = mx & s_mask;
  1370. uvsy = my & s_mask;
  1371. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1372. uvsrc_y =(s->mb_y*block_s>>field_based) + (my >> (lowres+1));
  1373. }
  1374. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1375. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1376. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1377. if( (unsigned)src_x > h_edge_pos - (!!sx) - 2*block_s
  1378. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1379. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  1380. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1381. ptr_y = s->edge_emu_buffer;
  1382. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1383. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  1384. ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
  1385. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1386. ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
  1387. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1388. ptr_cb= uvbuf;
  1389. ptr_cr= uvbuf+16;
  1390. }
  1391. }
  1392. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
  1393. dest_y += s->linesize;
  1394. dest_cb+= s->uvlinesize;
  1395. dest_cr+= s->uvlinesize;
  1396. }
  1397. if(field_select){
  1398. ptr_y += s->linesize;
  1399. ptr_cb+= s->uvlinesize;
  1400. ptr_cr+= s->uvlinesize;
  1401. }
  1402. sx <<= 2 - lowres;
  1403. sy <<= 2 - lowres;
  1404. pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy);
  1405. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1406. uvsx <<= 2 - lowres;
  1407. uvsy <<= 2 - lowres;
  1408. pix_op[lowres](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1409. pix_op[lowres](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1410. }
  1411. //FIXME h261 lowres loop filter
  1412. }
  1413. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1414. uint8_t *dest_cb, uint8_t *dest_cr,
  1415. uint8_t **ref_picture,
  1416. h264_chroma_mc_func *pix_op,
  1417. int mx, int my){
  1418. const int lowres= s->avctx->lowres;
  1419. const int block_s= 8>>lowres;
  1420. const int s_mask= (2<<lowres)-1;
  1421. const int h_edge_pos = s->h_edge_pos >> (lowres+1);
  1422. const int v_edge_pos = s->v_edge_pos >> (lowres+1);
  1423. int emu=0, src_x, src_y, offset, sx, sy;
  1424. uint8_t *ptr;
  1425. if(s->quarter_sample){
  1426. mx/=2;
  1427. my/=2;
  1428. }
  1429. /* In case of 8X8, we construct a single chroma motion vector
  1430. with a special rounding */
  1431. mx= ff_h263_round_chroma(mx);
  1432. my= ff_h263_round_chroma(my);
  1433. sx= mx & s_mask;
  1434. sy= my & s_mask;
  1435. src_x = s->mb_x*block_s + (mx >> (lowres+1));
  1436. src_y = s->mb_y*block_s + (my >> (lowres+1));
  1437. offset = src_y * s->uvlinesize + src_x;
  1438. ptr = ref_picture[1] + offset;
  1439. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1440. if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s
  1441. || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){
  1442. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1443. ptr= s->edge_emu_buffer;
  1444. emu=1;
  1445. }
  1446. }
  1447. sx <<= 2 - lowres;
  1448. sy <<= 2 - lowres;
  1449. pix_op[lowres](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1450. ptr = ref_picture[2] + offset;
  1451. if(emu){
  1452. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1453. ptr= s->edge_emu_buffer;
  1454. }
  1455. pix_op[lowres](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1456. }
  1457. /**
  1458. * motion compensation of a single macroblock
  1459. * @param s context
  1460. * @param dest_y luma destination pointer
  1461. * @param dest_cb chroma cb/u destination pointer
  1462. * @param dest_cr chroma cr/v destination pointer
  1463. * @param dir direction (0->forward, 1->backward)
  1464. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1465. * @param pic_op halfpel motion compensation function (average or put normally)
  1466. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1467. */
  1468. static inline void MPV_motion_lowres(MpegEncContext *s,
  1469. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1470. int dir, uint8_t **ref_picture,
  1471. h264_chroma_mc_func *pix_op)
  1472. {
  1473. int mx, my;
  1474. int mb_x, mb_y, i;
  1475. const int lowres= s->avctx->lowres;
  1476. const int block_s= 8>>lowres;
  1477. mb_x = s->mb_x;
  1478. mb_y = s->mb_y;
  1479. switch(s->mv_type) {
  1480. case MV_TYPE_16X16:
  1481. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1482. 0, 0, 0,
  1483. ref_picture, pix_op,
  1484. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
  1485. break;
  1486. case MV_TYPE_8X8:
  1487. mx = 0;
  1488. my = 0;
  1489. for(i=0;i<4;i++) {
  1490. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) * s->linesize)*block_s,
  1491. ref_picture[0], 0, 0,
  1492. (2*mb_x + (i & 1))*block_s, (2*mb_y + (i >>1))*block_s,
  1493. s->width, s->height, s->linesize,
  1494. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1495. block_s, block_s, pix_op,
  1496. s->mv[dir][i][0], s->mv[dir][i][1]);
  1497. mx += s->mv[dir][i][0];
  1498. my += s->mv[dir][i][1];
  1499. }
  1500. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  1501. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my);
  1502. break;
  1503. case MV_TYPE_FIELD:
  1504. if (s->picture_structure == PICT_FRAME) {
  1505. /* top field */
  1506. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1507. 1, 0, s->field_select[dir][0],
  1508. ref_picture, pix_op,
  1509. s->mv[dir][0][0], s->mv[dir][0][1], block_s);
  1510. /* bottom field */
  1511. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1512. 1, 1, s->field_select[dir][1],
  1513. ref_picture, pix_op,
  1514. s->mv[dir][1][0], s->mv[dir][1][1], block_s);
  1515. } else {
  1516. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){
  1517. ref_picture= s->current_picture_ptr->data;
  1518. }
  1519. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1520. 0, 0, s->field_select[dir][0],
  1521. ref_picture, pix_op,
  1522. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
  1523. }
  1524. break;
  1525. case MV_TYPE_16X8:
  1526. for(i=0; i<2; i++){
  1527. uint8_t ** ref2picture;
  1528. if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){
  1529. ref2picture= ref_picture;
  1530. }else{
  1531. ref2picture= s->current_picture_ptr->data;
  1532. }
  1533. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1534. 0, 0, s->field_select[dir][i],
  1535. ref2picture, pix_op,
  1536. s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s);
  1537. dest_y += 2*block_s*s->linesize;
  1538. dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1539. dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1540. }
  1541. break;
  1542. case MV_TYPE_DMV:
  1543. if(s->picture_structure == PICT_FRAME){
  1544. for(i=0; i<2; i++){
  1545. int j;
  1546. for(j=0; j<2; j++){
  1547. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1548. 1, j, j^i,
  1549. ref_picture, pix_op,
  1550. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], block_s);
  1551. }
  1552. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1553. }
  1554. }else{
  1555. for(i=0; i<2; i++){
  1556. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1557. 0, 0, s->picture_structure != i+1,
  1558. ref_picture, pix_op,
  1559. s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s);
  1560. // after put we make avg of the same block
  1561. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1562. //opposite parity is always in the same frame if this is second field
  1563. if(!s->first_field){
  1564. ref_picture = s->current_picture_ptr->data;
  1565. }
  1566. }
  1567. }
  1568. break;
  1569. default: assert(0);
  1570. }
  1571. }
  1572. /* put block[] to dest[] */
  1573. static inline void put_dct(MpegEncContext *s,
  1574. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1575. {
  1576. s->dct_unquantize_intra(s, block, i, qscale);
  1577. s->dsp.idct_put (dest, line_size, block);
  1578. }
  1579. /* add block[] to dest[] */
  1580. static inline void add_dct(MpegEncContext *s,
  1581. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1582. {
  1583. if (s->block_last_index[i] >= 0) {
  1584. s->dsp.idct_add (dest, line_size, block);
  1585. }
  1586. }
  1587. static inline void add_dequant_dct(MpegEncContext *s,
  1588. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1589. {
  1590. if (s->block_last_index[i] >= 0) {
  1591. s->dct_unquantize_inter(s, block, i, qscale);
  1592. s->dsp.idct_add (dest, line_size, block);
  1593. }
  1594. }
  1595. /**
  1596. * cleans dc, ac, coded_block for the current non intra MB
  1597. */
  1598. void ff_clean_intra_table_entries(MpegEncContext *s)
  1599. {
  1600. int wrap = s->b8_stride;
  1601. int xy = s->block_index[0];
  1602. s->dc_val[0][xy ] =
  1603. s->dc_val[0][xy + 1 ] =
  1604. s->dc_val[0][xy + wrap] =
  1605. s->dc_val[0][xy + 1 + wrap] = 1024;
  1606. /* ac pred */
  1607. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1608. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1609. if (s->msmpeg4_version>=3) {
  1610. s->coded_block[xy ] =
  1611. s->coded_block[xy + 1 ] =
  1612. s->coded_block[xy + wrap] =
  1613. s->coded_block[xy + 1 + wrap] = 0;
  1614. }
  1615. /* chroma */
  1616. wrap = s->mb_stride;
  1617. xy = s->mb_x + s->mb_y * wrap;
  1618. s->dc_val[1][xy] =
  1619. s->dc_val[2][xy] = 1024;
  1620. /* ac pred */
  1621. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1622. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1623. s->mbintra_table[xy]= 0;
  1624. }
  1625. /* generic function called after a macroblock has been parsed by the
  1626. decoder or after it has been encoded by the encoder.
  1627. Important variables used:
  1628. s->mb_intra : true if intra macroblock
  1629. s->mv_dir : motion vector direction
  1630. s->mv_type : motion vector type
  1631. s->mv : motion vector
  1632. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1633. */
  1634. static av_always_inline void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64], int lowres_flag)
  1635. {
  1636. int mb_x, mb_y;
  1637. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1638. #ifdef HAVE_XVMC
  1639. if(s->avctx->xvmc_acceleration){
  1640. XVMC_decode_mb(s);//xvmc uses pblocks
  1641. return;
  1642. }
  1643. #endif
  1644. mb_x = s->mb_x;
  1645. mb_y = s->mb_y;
  1646. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1647. /* save DCT coefficients */
  1648. int i,j;
  1649. DCTELEM *dct = &s->current_picture.dct_coeff[mb_xy*64*6];
  1650. for(i=0; i<6; i++)
  1651. for(j=0; j<64; j++)
  1652. *dct++ = block[i][s->dsp.idct_permutation[j]];
  1653. }
  1654. s->current_picture.qscale_table[mb_xy]= s->qscale;
  1655. /* update DC predictors for P macroblocks */
  1656. if (!s->mb_intra) {
  1657. if (s->h263_pred || s->h263_aic) {
  1658. if(s->mbintra_table[mb_xy])
  1659. ff_clean_intra_table_entries(s);
  1660. } else {
  1661. s->last_dc[0] =
  1662. s->last_dc[1] =
  1663. s->last_dc[2] = 128 << s->intra_dc_precision;
  1664. }
  1665. }
  1666. else if (s->h263_pred || s->h263_aic)
  1667. s->mbintra_table[mb_xy]=1;
  1668. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
  1669. uint8_t *dest_y, *dest_cb, *dest_cr;
  1670. int dct_linesize, dct_offset;
  1671. op_pixels_func (*op_pix)[4];
  1672. qpel_mc_func (*op_qpix)[16];
  1673. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
  1674. const int uvlinesize= s->current_picture.linesize[1];
  1675. const int readable= s->pict_type != B_TYPE || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  1676. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  1677. /* avoid copy if macroblock skipped in last frame too */
  1678. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1679. if(!s->encoding){
  1680. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1681. const int age= s->current_picture.age;
  1682. assert(age);
  1683. if (s->mb_skipped) {
  1684. s->mb_skipped= 0;
  1685. assert(s->pict_type!=I_TYPE);
  1686. (*mbskip_ptr) ++; /* indicate that this time we skipped it */
  1687. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1688. /* if previous was skipped too, then nothing to do ! */
  1689. if (*mbskip_ptr >= age && s->current_picture.reference){
  1690. return;
  1691. }
  1692. } else if(!s->current_picture.reference){
  1693. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  1694. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1695. } else{
  1696. *mbskip_ptr = 0; /* not skipped */
  1697. }
  1698. }
  1699. dct_linesize = linesize << s->interlaced_dct;
  1700. dct_offset =(s->interlaced_dct)? linesize : linesize*block_size;
  1701. if(readable){
  1702. dest_y= s->dest[0];
  1703. dest_cb= s->dest[1];
  1704. dest_cr= s->dest[2];
  1705. }else{
  1706. dest_y = s->b_scratchpad;
  1707. dest_cb= s->b_scratchpad+16*linesize;
  1708. dest_cr= s->b_scratchpad+32*linesize;
  1709. }
  1710. if (!s->mb_intra) {
  1711. /* motion handling */
  1712. /* decoding or more than one mb_type (MC was already done otherwise) */
  1713. if(!s->encoding){
  1714. if(lowres_flag){
  1715. h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
  1716. if (s->mv_dir & MV_DIR_FORWARD) {
  1717. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix);
  1718. op_pix = s->dsp.avg_h264_chroma_pixels_tab;
  1719. }
  1720. if (s->mv_dir & MV_DIR_BACKWARD) {
  1721. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix);
  1722. }
  1723. }else{
  1724. op_qpix= s->me.qpel_put;
  1725. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1726. op_pix = s->dsp.put_pixels_tab;
  1727. }else{
  1728. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1729. }
  1730. if (s->mv_dir & MV_DIR_FORWARD) {
  1731. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  1732. op_pix = s->dsp.avg_pixels_tab;
  1733. op_qpix= s->me.qpel_avg;
  1734. }
  1735. if (s->mv_dir & MV_DIR_BACKWARD) {
  1736. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  1737. }
  1738. }
  1739. }
  1740. /* skip dequant / idct if we are really late ;) */
  1741. if(s->hurry_up>1) goto skip_idct;
  1742. if(s->avctx->skip_idct){
  1743. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == B_TYPE)
  1744. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != I_TYPE)
  1745. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1746. goto skip_idct;
  1747. }
  1748. /* add dct residue */
  1749. if(s->encoding || !( s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  1750. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1751. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1752. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1753. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1754. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1755. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1756. if (s->chroma_y_shift){
  1757. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1758. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1759. }else{
  1760. dct_linesize >>= 1;
  1761. dct_offset >>=1;
  1762. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1763. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1764. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1765. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1766. }
  1767. }
  1768. } else if(s->codec_id != CODEC_ID_WMV2){
  1769. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1770. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1771. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1772. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1773. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1774. if(s->chroma_y_shift){//Chroma420
  1775. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1776. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1777. }else{
  1778. //chroma422
  1779. dct_linesize = uvlinesize << s->interlaced_dct;
  1780. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  1781. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1782. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1783. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1784. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1785. if(!s->chroma_x_shift){//Chroma444
  1786. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  1787. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  1788. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  1789. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  1790. }
  1791. }
  1792. }//fi gray
  1793. }
  1794. else if (ENABLE_WMV2) {
  1795. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1796. }
  1797. } else {
  1798. /* dct only in intra block */
  1799. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  1800. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1801. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1802. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1803. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1804. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1805. if(s->chroma_y_shift){
  1806. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1807. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1808. }else{
  1809. dct_offset >>=1;
  1810. dct_linesize >>=1;
  1811. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1812. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1813. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1814. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1815. }
  1816. }
  1817. }else{
  1818. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1819. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1820. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1821. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1822. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1823. if(s->chroma_y_shift){
  1824. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1825. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1826. }else{
  1827. dct_linesize = uvlinesize << s->interlaced_dct;
  1828. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  1829. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  1830. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  1831. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1832. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1833. if(!s->chroma_x_shift){//Chroma444
  1834. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  1835. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  1836. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  1837. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  1838. }
  1839. }
  1840. }//gray
  1841. }
  1842. }
  1843. skip_idct:
  1844. if(!readable){
  1845. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1846. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1847. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1848. }
  1849. }
  1850. }
  1851. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
  1852. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1);
  1853. else MPV_decode_mb_internal(s, block, 0);
  1854. }
  1855. /**
  1856. *
  1857. * @param h is the normal height, this will be reduced automatically if needed for the last row
  1858. */
  1859. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  1860. if (s->avctx->draw_horiz_band) {
  1861. AVFrame *src;
  1862. int offset[4];
  1863. if(s->picture_structure != PICT_FRAME){
  1864. h <<= 1;
  1865. y <<= 1;
  1866. if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  1867. }
  1868. h= FFMIN(h, s->avctx->height - y);
  1869. if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  1870. src= (AVFrame*)s->current_picture_ptr;
  1871. else if(s->last_picture_ptr)
  1872. src= (AVFrame*)s->last_picture_ptr;
  1873. else
  1874. return;
  1875. if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  1876. offset[0]=
  1877. offset[1]=
  1878. offset[2]=
  1879. offset[3]= 0;
  1880. }else{
  1881. offset[0]= y * s->linesize;;
  1882. offset[1]=
  1883. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  1884. offset[3]= 0;
  1885. }
  1886. emms_c();
  1887. s->avctx->draw_horiz_band(s->avctx, src, offset,
  1888. y, s->picture_structure, h);
  1889. }
  1890. }
  1891. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  1892. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
  1893. const int uvlinesize= s->current_picture.linesize[1];
  1894. const int mb_size= 4 - s->avctx->lowres;
  1895. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  1896. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  1897. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  1898. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  1899. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  1900. s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  1901. //block_index is not used by mpeg2, so it is not affected by chroma_format
  1902. s->dest[0] = s->current_picture.data[0] + ((s->mb_x - 1) << mb_size);
  1903. s->dest[1] = s->current_picture.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  1904. s->dest[2] = s->current_picture.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  1905. if(!(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  1906. {
  1907. s->dest[0] += s->mb_y * linesize << mb_size;
  1908. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1909. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1910. }
  1911. }
  1912. void ff_mpeg_flush(AVCodecContext *avctx){
  1913. int i;
  1914. MpegEncContext *s = avctx->priv_data;
  1915. if(s==NULL || s->picture==NULL)
  1916. return;
  1917. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1918. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  1919. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  1920. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  1921. }
  1922. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  1923. s->mb_x= s->mb_y= 0;
  1924. s->parse_context.state= -1;
  1925. s->parse_context.frame_start_found= 0;
  1926. s->parse_context.overread= 0;
  1927. s->parse_context.overread_index= 0;
  1928. s->parse_context.index= 0;
  1929. s->parse_context.last_index= 0;
  1930. s->bitstream_buffer_size=0;
  1931. s->pp_time=0;
  1932. }
  1933. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  1934. DCTELEM *block, int n, int qscale)
  1935. {
  1936. int i, level, nCoeffs;
  1937. const uint16_t *quant_matrix;
  1938. nCoeffs= s->block_last_index[n];
  1939. if (n < 4)
  1940. block[0] = block[0] * s->y_dc_scale;
  1941. else
  1942. block[0] = block[0] * s->c_dc_scale;
  1943. /* XXX: only mpeg1 */
  1944. quant_matrix = s->intra_matrix;
  1945. for(i=1;i<=nCoeffs;i++) {
  1946. int j= s->intra_scantable.permutated[i];
  1947. level = block[j];
  1948. if (level) {
  1949. if (level < 0) {
  1950. level = -level;
  1951. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1952. level = (level - 1) | 1;
  1953. level = -level;
  1954. } else {
  1955. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1956. level = (level - 1) | 1;
  1957. }
  1958. block[j] = level;
  1959. }
  1960. }
  1961. }
  1962. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  1963. DCTELEM *block, int n, int qscale)
  1964. {
  1965. int i, level, nCoeffs;
  1966. const uint16_t *quant_matrix;
  1967. nCoeffs= s->block_last_index[n];
  1968. quant_matrix = s->inter_matrix;
  1969. for(i=0; i<=nCoeffs; i++) {
  1970. int j= s->intra_scantable.permutated[i];
  1971. level = block[j];
  1972. if (level) {
  1973. if (level < 0) {
  1974. level = -level;
  1975. level = (((level << 1) + 1) * qscale *
  1976. ((int) (quant_matrix[j]))) >> 4;
  1977. level = (level - 1) | 1;
  1978. level = -level;
  1979. } else {
  1980. level = (((level << 1) + 1) * qscale *
  1981. ((int) (quant_matrix[j]))) >> 4;
  1982. level = (level - 1) | 1;
  1983. }
  1984. block[j] = level;
  1985. }
  1986. }
  1987. }
  1988. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  1989. DCTELEM *block, int n, int qscale)
  1990. {
  1991. int i, level, nCoeffs;
  1992. const uint16_t *quant_matrix;
  1993. if(s->alternate_scan) nCoeffs= 63;
  1994. else nCoeffs= s->block_last_index[n];
  1995. if (n < 4)
  1996. block[0] = block[0] * s->y_dc_scale;
  1997. else
  1998. block[0] = block[0] * s->c_dc_scale;
  1999. quant_matrix = s->intra_matrix;
  2000. for(i=1;i<=nCoeffs;i++) {
  2001. int j= s->intra_scantable.permutated[i];
  2002. level = block[j];
  2003. if (level) {
  2004. if (level < 0) {
  2005. level = -level;
  2006. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2007. level = -level;
  2008. } else {
  2009. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2010. }
  2011. block[j] = level;
  2012. }
  2013. }
  2014. }
  2015. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2016. DCTELEM *block, int n, int qscale)
  2017. {
  2018. int i, level, nCoeffs;
  2019. const uint16_t *quant_matrix;
  2020. int sum=-1;
  2021. if(s->alternate_scan) nCoeffs= 63;
  2022. else nCoeffs= s->block_last_index[n];
  2023. if (n < 4)
  2024. block[0] = block[0] * s->y_dc_scale;
  2025. else
  2026. block[0] = block[0] * s->c_dc_scale;
  2027. quant_matrix = s->intra_matrix;
  2028. for(i=1;i<=nCoeffs;i++) {
  2029. int j= s->intra_scantable.permutated[i];
  2030. level = block[j];
  2031. if (level) {
  2032. if (level < 0) {
  2033. level = -level;
  2034. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2035. level = -level;
  2036. } else {
  2037. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2038. }
  2039. block[j] = level;
  2040. sum+=level;
  2041. }
  2042. }
  2043. block[63]^=sum&1;
  2044. }
  2045. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2046. DCTELEM *block, int n, int qscale)
  2047. {
  2048. int i, level, nCoeffs;
  2049. const uint16_t *quant_matrix;
  2050. int sum=-1;
  2051. if(s->alternate_scan) nCoeffs= 63;
  2052. else nCoeffs= s->block_last_index[n];
  2053. quant_matrix = s->inter_matrix;
  2054. for(i=0; i<=nCoeffs; i++) {
  2055. int j= s->intra_scantable.permutated[i];
  2056. level = block[j];
  2057. if (level) {
  2058. if (level < 0) {
  2059. level = -level;
  2060. level = (((level << 1) + 1) * qscale *
  2061. ((int) (quant_matrix[j]))) >> 4;
  2062. level = -level;
  2063. } else {
  2064. level = (((level << 1) + 1) * qscale *
  2065. ((int) (quant_matrix[j]))) >> 4;
  2066. }
  2067. block[j] = level;
  2068. sum+=level;
  2069. }
  2070. }
  2071. block[63]^=sum&1;
  2072. }
  2073. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2074. DCTELEM *block, int n, int qscale)
  2075. {
  2076. int i, level, qmul, qadd;
  2077. int nCoeffs;
  2078. assert(s->block_last_index[n]>=0);
  2079. qmul = qscale << 1;
  2080. if (!s->h263_aic) {
  2081. if (n < 4)
  2082. block[0] = block[0] * s->y_dc_scale;
  2083. else
  2084. block[0] = block[0] * s->c_dc_scale;
  2085. qadd = (qscale - 1) | 1;
  2086. }else{
  2087. qadd = 0;
  2088. }
  2089. if(s->ac_pred)
  2090. nCoeffs=63;
  2091. else
  2092. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2093. for(i=1; i<=nCoeffs; i++) {
  2094. level = block[i];
  2095. if (level) {
  2096. if (level < 0) {
  2097. level = level * qmul - qadd;
  2098. } else {
  2099. level = level * qmul + qadd;
  2100. }
  2101. block[i] = level;
  2102. }
  2103. }
  2104. }
  2105. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2106. DCTELEM *block, int n, int qscale)
  2107. {
  2108. int i, level, qmul, qadd;
  2109. int nCoeffs;
  2110. assert(s->block_last_index[n]>=0);
  2111. qadd = (qscale - 1) | 1;
  2112. qmul = qscale << 1;
  2113. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2114. for(i=0; i<=nCoeffs; i++) {
  2115. level = block[i];
  2116. if (level) {
  2117. if (level < 0) {
  2118. level = level * qmul - qadd;
  2119. } else {
  2120. level = level * qmul + qadd;
  2121. }
  2122. block[i] = level;
  2123. }
  2124. }
  2125. }
  2126. /**
  2127. * set qscale and update qscale dependent variables.
  2128. */
  2129. void ff_set_qscale(MpegEncContext * s, int qscale)
  2130. {
  2131. if (qscale < 1)
  2132. qscale = 1;
  2133. else if (qscale > 31)
  2134. qscale = 31;
  2135. s->qscale = qscale;
  2136. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2137. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2138. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2139. }