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.

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