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.

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