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.

2359 lines
88KB

  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. CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
  211. CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
  212. CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
  213. }
  214. CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
  215. CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
  216. CHECKED_ALLOCZ(pic->mb_type_base , (big_mb_num + s->mb_stride) * sizeof(uint32_t))
  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. CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+4) * sizeof(int16_t))
  221. pic->motion_val[i]= pic->motion_val_base[i]+4;
  222. CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
  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. CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+4) * sizeof(int16_t))
  228. pic->motion_val[i]= pic->motion_val_base[i]+4;
  229. CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
  230. }
  231. pic->motion_subsample_log2= 3;
  232. }
  233. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  234. CHECKED_ALLOCZ(pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6)
  235. }
  236. pic->qstride= s->mb_stride;
  237. CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
  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 CHECKED_ALLOCZ 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. CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*21*2); //(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. CHECKED_ALLOCZ(s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t))
  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. CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
  293. CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
  294. if(s->avctx->noise_reduction){
  295. CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
  296. }
  297. }
  298. CHECKED_ALLOCZ(s->blocks, 64*12*2 * sizeof(DCTELEM))
  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. CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //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. CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  448. CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  449. CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  450. CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  451. CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  452. CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  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. CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
  461. }
  462. CHECKED_ALLOCZ(s->avctx->stats_out, 256);
  463. /* Allocate MB type table */
  464. CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint16_t)) //needed for encoding
  465. CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
  466. CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
  467. CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
  468. CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
  469. CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
  470. CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
  471. CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
  472. if(s->avctx->noise_reduction){
  473. CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
  474. }
  475. }
  476. CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
  477. for(i = 0; i < MAX_PICTURE_COUNT; i++) {
  478. avcodec_get_frame_defaults((AVFrame *)&s->picture[i]);
  479. }
  480. CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
  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. CHECKED_ALLOCZ(s->b_field_mv_table_base[i][j][k] , mv_table_size * 2 * sizeof(int16_t))
  488. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
  489. }
  490. CHECKED_ALLOCZ(s->b_field_select_table[i][j] , mb_array_size * 2 * sizeof(uint8_t))
  491. CHECKED_ALLOCZ(s->p_field_mv_table_base[i][j] , mv_table_size * 2 * sizeof(int16_t))
  492. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
  493. }
  494. CHECKED_ALLOCZ(s->p_field_select_table[i] , mb_array_size * 2 * sizeof(uint8_t))
  495. }
  496. }
  497. if (s->out_format == FMT_H263) {
  498. /* ac values */
  499. CHECKED_ALLOCZ(s->ac_val_base, yc_size * sizeof(int16_t) * 16);
  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. CHECKED_ALLOCZ(s->coded_block_base, y_size);
  505. s->coded_block= s->coded_block_base + s->b8_stride + 1;
  506. /* cbp, ac_pred, pred_dir */
  507. CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
  508. CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
  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. CHECKED_ALLOCZ(s->dc_val_base, yc_size * sizeof(int16_t));
  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. CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
  522. memset(s->mbintra_table, 1, mb_array_size);
  523. /* init macroblock skip table */
  524. CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
  525. //Note the +1 is for a quicker mpeg4 slice_end detection
  526. CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
  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. alloc:
  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->last_picture_ptr) ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  820. if(s->next_picture_ptr) ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  821. if(s->pict_type != FF_I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL) && !s->dropable && s->codec_id != CODEC_ID_H264){
  822. av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
  823. assert(s->pict_type != FF_B_TYPE); //these should have been dropped if we don't have a reference
  824. goto alloc;
  825. }
  826. assert(s->pict_type == FF_I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
  827. if(s->picture_structure!=PICT_FRAME && s->out_format != FMT_H264){
  828. int i;
  829. for(i=0; i<4; i++){
  830. if(s->picture_structure == PICT_BOTTOM_FIELD){
  831. s->current_picture.data[i] += s->current_picture.linesize[i];
  832. }
  833. s->current_picture.linesize[i] *= 2;
  834. s->last_picture.linesize[i] *=2;
  835. s->next_picture.linesize[i] *=2;
  836. }
  837. }
  838. s->hurry_up= s->avctx->hurry_up;
  839. s->error_recognition= avctx->error_recognition;
  840. /* set dequantizer, we can't do it during init as it might change for mpeg4
  841. and we can't do it in the header decode as init is not called for mpeg4 there yet */
  842. if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
  843. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  844. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  845. }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  846. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  847. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  848. }else{
  849. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  850. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  851. }
  852. if(s->dct_error_sum){
  853. assert(s->avctx->noise_reduction && s->encoding);
  854. update_noise_reduction(s);
  855. }
  856. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  857. return ff_xvmc_field_start(s, avctx);
  858. return 0;
  859. }
  860. /* generic function for encode/decode called after a frame has been coded/decoded */
  861. void MPV_frame_end(MpegEncContext *s)
  862. {
  863. int i;
  864. /* draw edge for correct motion prediction if outside */
  865. //just to make sure that all data is rendered.
  866. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  867. ff_xvmc_field_end(s);
  868. }else if(!s->avctx->hwaccel
  869. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  870. && s->unrestricted_mv
  871. && s->current_picture.reference
  872. && !s->intra_only
  873. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  874. s->dsp.draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
  875. s->dsp.draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  876. s->dsp.draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  877. }
  878. emms_c();
  879. s->last_pict_type = s->pict_type;
  880. s->last_lambda_for[s->pict_type]= s->current_picture_ptr->quality;
  881. if(s->pict_type!=FF_B_TYPE){
  882. s->last_non_b_pict_type= s->pict_type;
  883. }
  884. #if 0
  885. /* copy back current_picture variables */
  886. for(i=0; i<MAX_PICTURE_COUNT; i++){
  887. if(s->picture[i].data[0] == s->current_picture.data[0]){
  888. s->picture[i]= s->current_picture;
  889. break;
  890. }
  891. }
  892. assert(i<MAX_PICTURE_COUNT);
  893. #endif
  894. if(s->encoding){
  895. /* release non-reference frames */
  896. for(i=0; i<MAX_PICTURE_COUNT; i++){
  897. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  898. free_frame_buffer(s, &s->picture[i]);
  899. }
  900. }
  901. }
  902. // clear copies, to avoid confusion
  903. #if 0
  904. memset(&s->last_picture, 0, sizeof(Picture));
  905. memset(&s->next_picture, 0, sizeof(Picture));
  906. memset(&s->current_picture, 0, sizeof(Picture));
  907. #endif
  908. s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
  909. }
  910. /**
  911. * draws an line from (ex, ey) -> (sx, sy).
  912. * @param w width of the image
  913. * @param h height of the image
  914. * @param stride stride/linesize of the image
  915. * @param color color of the arrow
  916. */
  917. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  918. int x, y, fr, f;
  919. sx= av_clip(sx, 0, w-1);
  920. sy= av_clip(sy, 0, h-1);
  921. ex= av_clip(ex, 0, w-1);
  922. ey= av_clip(ey, 0, h-1);
  923. buf[sy*stride + sx]+= color;
  924. if(FFABS(ex - sx) > FFABS(ey - sy)){
  925. if(sx > ex){
  926. FFSWAP(int, sx, ex);
  927. FFSWAP(int, sy, ey);
  928. }
  929. buf+= sx + sy*stride;
  930. ex-= sx;
  931. f= ((ey-sy)<<16)/ex;
  932. for(x= 0; x <= ex; x++){
  933. y = (x*f)>>16;
  934. fr= (x*f)&0xFFFF;
  935. buf[ y *stride + x]+= (color*(0x10000-fr))>>16;
  936. buf[(y+1)*stride + x]+= (color* fr )>>16;
  937. }
  938. }else{
  939. if(sy > ey){
  940. FFSWAP(int, sx, ex);
  941. FFSWAP(int, sy, ey);
  942. }
  943. buf+= sx + sy*stride;
  944. ey-= sy;
  945. if(ey) f= ((ex-sx)<<16)/ey;
  946. else f= 0;
  947. for(y= 0; y <= ey; y++){
  948. x = (y*f)>>16;
  949. fr= (y*f)&0xFFFF;
  950. buf[y*stride + x ]+= (color*(0x10000-fr))>>16;
  951. buf[y*stride + x+1]+= (color* fr )>>16;
  952. }
  953. }
  954. }
  955. /**
  956. * draws an arrow from (ex, ey) -> (sx, sy).
  957. * @param w width of the image
  958. * @param h height of the image
  959. * @param stride stride/linesize of the image
  960. * @param color color of the arrow
  961. */
  962. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  963. int dx,dy;
  964. sx= av_clip(sx, -100, w+100);
  965. sy= av_clip(sy, -100, h+100);
  966. ex= av_clip(ex, -100, w+100);
  967. ey= av_clip(ey, -100, h+100);
  968. dx= ex - sx;
  969. dy= ey - sy;
  970. if(dx*dx + dy*dy > 3*3){
  971. int rx= dx + dy;
  972. int ry= -dx + dy;
  973. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  974. //FIXME subpixel accuracy
  975. rx= ROUNDED_DIV(rx*3<<4, length);
  976. ry= ROUNDED_DIV(ry*3<<4, length);
  977. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  978. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  979. }
  980. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  981. }
  982. /**
  983. * prints debuging info for the given picture.
  984. */
  985. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
  986. if(s->avctx->hwaccel || !pict || !pict->mb_type) return;
  987. if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
  988. int x,y;
  989. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  990. switch (pict->pict_type) {
  991. case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
  992. case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
  993. case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
  994. case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
  995. case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
  996. case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;
  997. }
  998. for(y=0; y<s->mb_height; y++){
  999. for(x=0; x<s->mb_width; x++){
  1000. if(s->avctx->debug&FF_DEBUG_SKIP){
  1001. int count= s->mbskip_table[x + y*s->mb_stride];
  1002. if(count>9) count=9;
  1003. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1004. }
  1005. if(s->avctx->debug&FF_DEBUG_QP){
  1006. av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
  1007. }
  1008. if(s->avctx->debug&FF_DEBUG_MB_TYPE){
  1009. int mb_type= pict->mb_type[x + y*s->mb_stride];
  1010. //Type & MV direction
  1011. if(IS_PCM(mb_type))
  1012. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1013. else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1014. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1015. else if(IS_INTRA4x4(mb_type))
  1016. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1017. else if(IS_INTRA16x16(mb_type))
  1018. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1019. else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1020. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1021. else if(IS_DIRECT(mb_type))
  1022. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1023. else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
  1024. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1025. else if(IS_GMC(mb_type))
  1026. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1027. else if(IS_SKIP(mb_type))
  1028. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1029. else if(!USES_LIST(mb_type, 1))
  1030. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1031. else if(!USES_LIST(mb_type, 0))
  1032. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1033. else{
  1034. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1035. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1036. }
  1037. //segmentation
  1038. if(IS_8X8(mb_type))
  1039. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1040. else if(IS_16X8(mb_type))
  1041. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1042. else if(IS_8X16(mb_type))
  1043. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1044. else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
  1045. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1046. else
  1047. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1048. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
  1049. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1050. else
  1051. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1052. }
  1053. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1054. }
  1055. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1056. }
  1057. }
  1058. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  1059. const int shift= 1 + s->quarter_sample;
  1060. int mb_y;
  1061. uint8_t *ptr;
  1062. int i;
  1063. int h_chroma_shift, v_chroma_shift, block_height;
  1064. const int width = s->avctx->width;
  1065. const int height= s->avctx->height;
  1066. const int mv_sample_log2= 4 - pict->motion_subsample_log2;
  1067. const int mv_stride= (s->mb_width << mv_sample_log2) + (s->codec_id == CODEC_ID_H264 ? 0 : 1);
  1068. s->low_delay=0; //needed to see the vectors without trashing the buffers
  1069. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1070. for(i=0; i<3; i++){
  1071. memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*height:pict->linesize[i]*height >> v_chroma_shift);
  1072. pict->data[i]= s->visualization_buffer[i];
  1073. }
  1074. pict->type= FF_BUFFER_TYPE_COPY;
  1075. ptr= pict->data[0];
  1076. block_height = 16>>v_chroma_shift;
  1077. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  1078. int mb_x;
  1079. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  1080. const int mb_index= mb_x + mb_y*s->mb_stride;
  1081. if((s->avctx->debug_mv) && pict->motion_val){
  1082. int type;
  1083. for(type=0; type<3; type++){
  1084. int direction = 0;
  1085. switch (type) {
  1086. case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=FF_P_TYPE))
  1087. continue;
  1088. direction = 0;
  1089. break;
  1090. case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=FF_B_TYPE))
  1091. continue;
  1092. direction = 0;
  1093. break;
  1094. case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=FF_B_TYPE))
  1095. continue;
  1096. direction = 1;
  1097. break;
  1098. }
  1099. if(!USES_LIST(pict->mb_type[mb_index], direction))
  1100. continue;
  1101. if(IS_8X8(pict->mb_type[mb_index])){
  1102. int i;
  1103. for(i=0; i<4; i++){
  1104. int sx= mb_x*16 + 4 + 8*(i&1);
  1105. int sy= mb_y*16 + 4 + 8*(i>>1);
  1106. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1107. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1108. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1109. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1110. }
  1111. }else if(IS_16X8(pict->mb_type[mb_index])){
  1112. int i;
  1113. for(i=0; i<2; i++){
  1114. int sx=mb_x*16 + 8;
  1115. int sy=mb_y*16 + 4 + 8*i;
  1116. int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << (mv_sample_log2-1);
  1117. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1118. int my=(pict->motion_val[direction][xy][1]>>shift);
  1119. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1120. my*=2;
  1121. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1122. }
  1123. }else if(IS_8X16(pict->mb_type[mb_index])){
  1124. int i;
  1125. for(i=0; i<2; i++){
  1126. int sx=mb_x*16 + 4 + 8*i;
  1127. int sy=mb_y*16 + 8;
  1128. int xy= (mb_x*2 + i + mb_y*2*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{
  1136. int sx= mb_x*16 + 8;
  1137. int sy= mb_y*16 + 8;
  1138. int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2;
  1139. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1140. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1141. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1142. }
  1143. }
  1144. }
  1145. if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
  1146. uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
  1147. int y;
  1148. for(y=0; y<block_height; y++){
  1149. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= c;
  1150. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= c;
  1151. }
  1152. }
  1153. if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
  1154. int mb_type= pict->mb_type[mb_index];
  1155. uint64_t u,v;
  1156. int y;
  1157. #define COLOR(theta, r)\
  1158. u= (int)(128 + r*cos(theta*3.141592/180));\
  1159. v= (int)(128 + r*sin(theta*3.141592/180));
  1160. u=v=128;
  1161. if(IS_PCM(mb_type)){
  1162. COLOR(120,48)
  1163. }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
  1164. COLOR(30,48)
  1165. }else if(IS_INTRA4x4(mb_type)){
  1166. COLOR(90,48)
  1167. }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
  1168. // COLOR(120,48)
  1169. }else if(IS_DIRECT(mb_type)){
  1170. COLOR(150,48)
  1171. }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
  1172. COLOR(170,48)
  1173. }else if(IS_GMC(mb_type)){
  1174. COLOR(190,48)
  1175. }else if(IS_SKIP(mb_type)){
  1176. // COLOR(180,48)
  1177. }else if(!USES_LIST(mb_type, 1)){
  1178. COLOR(240,48)
  1179. }else if(!USES_LIST(mb_type, 0)){
  1180. COLOR(0,48)
  1181. }else{
  1182. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1183. COLOR(300,48)
  1184. }
  1185. u*= 0x0101010101010101ULL;
  1186. v*= 0x0101010101010101ULL;
  1187. for(y=0; y<block_height; y++){
  1188. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= u;
  1189. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= v;
  1190. }
  1191. //segmentation
  1192. if(IS_8X8(mb_type) || IS_16X8(mb_type)){
  1193. *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1194. *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1195. }
  1196. if(IS_8X8(mb_type) || IS_8X16(mb_type)){
  1197. for(y=0; y<16; y++)
  1198. pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
  1199. }
  1200. if(IS_8X8(mb_type) && mv_sample_log2 >= 2){
  1201. int dm= 1 << (mv_sample_log2-2);
  1202. for(i=0; i<4; i++){
  1203. int sx= mb_x*16 + 8*(i&1);
  1204. int sy= mb_y*16 + 8*(i>>1);
  1205. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1206. //FIXME bidir
  1207. int32_t *mv = (int32_t*)&pict->motion_val[0][xy];
  1208. if(mv[0] != mv[dm] || mv[dm*mv_stride] != mv[dm*(mv_stride+1)])
  1209. for(y=0; y<8; y++)
  1210. pict->data[0][sx + 4 + (sy + y)*pict->linesize[0]]^= 0x80;
  1211. if(mv[0] != mv[dm*mv_stride] || mv[dm] != mv[dm*(mv_stride+1)])
  1212. *(uint64_t*)(pict->data[0] + sx + (sy + 4)*pict->linesize[0])^= 0x8080808080808080ULL;
  1213. }
  1214. }
  1215. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
  1216. // hmm
  1217. }
  1218. }
  1219. s->mbskip_table[mb_index]=0;
  1220. }
  1221. }
  1222. }
  1223. }
  1224. static inline int hpel_motion_lowres(MpegEncContext *s,
  1225. uint8_t *dest, uint8_t *src,
  1226. int field_based, int field_select,
  1227. int src_x, int src_y,
  1228. int width, int height, int stride,
  1229. int h_edge_pos, int v_edge_pos,
  1230. int w, int h, h264_chroma_mc_func *pix_op,
  1231. int motion_x, int motion_y)
  1232. {
  1233. const int lowres= s->avctx->lowres;
  1234. const int s_mask= (2<<lowres)-1;
  1235. int emu=0;
  1236. int sx, sy;
  1237. if(s->quarter_sample){
  1238. motion_x/=2;
  1239. motion_y/=2;
  1240. }
  1241. sx= motion_x & s_mask;
  1242. sy= motion_y & s_mask;
  1243. src_x += motion_x >> (lowres+1);
  1244. src_y += motion_y >> (lowres+1);
  1245. src += src_y * stride + src_x;
  1246. if( (unsigned)src_x > h_edge_pos - (!!sx) - w
  1247. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1248. ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  1249. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1250. src= s->edge_emu_buffer;
  1251. emu=1;
  1252. }
  1253. sx <<= 2 - lowres;
  1254. sy <<= 2 - lowres;
  1255. if(field_select)
  1256. src += s->linesize;
  1257. pix_op[lowres](dest, src, stride, h, sx, sy);
  1258. return emu;
  1259. }
  1260. /* apply one mpeg motion vector to the three components */
  1261. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1262. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1263. int field_based, int bottom_field, int field_select,
  1264. uint8_t **ref_picture, h264_chroma_mc_func *pix_op,
  1265. int motion_x, int motion_y, int h)
  1266. {
  1267. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1268. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy, uvsx, uvsy;
  1269. const int lowres= s->avctx->lowres;
  1270. const int block_s= 8>>lowres;
  1271. const int s_mask= (2<<lowres)-1;
  1272. const int h_edge_pos = s->h_edge_pos >> lowres;
  1273. const int v_edge_pos = s->v_edge_pos >> lowres;
  1274. linesize = s->current_picture.linesize[0] << field_based;
  1275. uvlinesize = s->current_picture.linesize[1] << field_based;
  1276. if(s->quarter_sample){ //FIXME obviously not perfect but qpel will not work in lowres anyway
  1277. motion_x/=2;
  1278. motion_y/=2;
  1279. }
  1280. if(field_based){
  1281. motion_y += (bottom_field - field_select)*((1<<lowres)-1);
  1282. }
  1283. sx= motion_x & s_mask;
  1284. sy= motion_y & s_mask;
  1285. src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1));
  1286. src_y =(s->mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1));
  1287. if (s->out_format == FMT_H263) {
  1288. uvsx = ((motion_x>>1) & s_mask) | (sx&1);
  1289. uvsy = ((motion_y>>1) & s_mask) | (sy&1);
  1290. uvsrc_x = src_x>>1;
  1291. uvsrc_y = src_y>>1;
  1292. }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  1293. mx = motion_x / 4;
  1294. my = motion_y / 4;
  1295. uvsx = (2*mx) & s_mask;
  1296. uvsy = (2*my) & s_mask;
  1297. uvsrc_x = s->mb_x*block_s + (mx >> lowres);
  1298. uvsrc_y = s->mb_y*block_s + (my >> lowres);
  1299. } else {
  1300. mx = motion_x / 2;
  1301. my = motion_y / 2;
  1302. uvsx = mx & s_mask;
  1303. uvsy = my & s_mask;
  1304. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1305. uvsrc_y =(s->mb_y*block_s>>field_based) + (my >> (lowres+1));
  1306. }
  1307. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1308. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1309. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1310. if( (unsigned)src_x > h_edge_pos - (!!sx) - 2*block_s
  1311. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1312. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  1313. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1314. ptr_y = s->edge_emu_buffer;
  1315. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1316. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  1317. ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
  1318. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1319. ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
  1320. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1321. ptr_cb= uvbuf;
  1322. ptr_cr= uvbuf+16;
  1323. }
  1324. }
  1325. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
  1326. dest_y += s->linesize;
  1327. dest_cb+= s->uvlinesize;
  1328. dest_cr+= s->uvlinesize;
  1329. }
  1330. if(field_select){
  1331. ptr_y += s->linesize;
  1332. ptr_cb+= s->uvlinesize;
  1333. ptr_cr+= s->uvlinesize;
  1334. }
  1335. sx <<= 2 - lowres;
  1336. sy <<= 2 - lowres;
  1337. pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy);
  1338. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1339. uvsx <<= 2 - lowres;
  1340. uvsy <<= 2 - lowres;
  1341. pix_op[lowres](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1342. pix_op[lowres](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1343. }
  1344. //FIXME h261 lowres loop filter
  1345. }
  1346. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1347. uint8_t *dest_cb, uint8_t *dest_cr,
  1348. uint8_t **ref_picture,
  1349. h264_chroma_mc_func *pix_op,
  1350. int mx, int my){
  1351. const int lowres= s->avctx->lowres;
  1352. const int block_s= 8>>lowres;
  1353. const int s_mask= (2<<lowres)-1;
  1354. const int h_edge_pos = s->h_edge_pos >> (lowres+1);
  1355. const int v_edge_pos = s->v_edge_pos >> (lowres+1);
  1356. int emu=0, src_x, src_y, offset, sx, sy;
  1357. uint8_t *ptr;
  1358. if(s->quarter_sample){
  1359. mx/=2;
  1360. my/=2;
  1361. }
  1362. /* In case of 8X8, we construct a single chroma motion vector
  1363. with a special rounding */
  1364. mx= ff_h263_round_chroma(mx);
  1365. my= ff_h263_round_chroma(my);
  1366. sx= mx & s_mask;
  1367. sy= my & s_mask;
  1368. src_x = s->mb_x*block_s + (mx >> (lowres+1));
  1369. src_y = s->mb_y*block_s + (my >> (lowres+1));
  1370. offset = src_y * s->uvlinesize + src_x;
  1371. ptr = ref_picture[1] + offset;
  1372. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1373. if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s
  1374. || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){
  1375. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1376. ptr= s->edge_emu_buffer;
  1377. emu=1;
  1378. }
  1379. }
  1380. sx <<= 2 - lowres;
  1381. sy <<= 2 - lowres;
  1382. pix_op[lowres](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1383. ptr = ref_picture[2] + offset;
  1384. if(emu){
  1385. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1386. ptr= s->edge_emu_buffer;
  1387. }
  1388. pix_op[lowres](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1389. }
  1390. /**
  1391. * motion compensation of a single macroblock
  1392. * @param s context
  1393. * @param dest_y luma destination pointer
  1394. * @param dest_cb chroma cb/u destination pointer
  1395. * @param dest_cr chroma cr/v destination pointer
  1396. * @param dir direction (0->forward, 1->backward)
  1397. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1398. * @param pic_op halfpel motion compensation function (average or put normally)
  1399. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1400. */
  1401. static inline void MPV_motion_lowres(MpegEncContext *s,
  1402. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1403. int dir, uint8_t **ref_picture,
  1404. h264_chroma_mc_func *pix_op)
  1405. {
  1406. int mx, my;
  1407. int mb_x, mb_y, i;
  1408. const int lowres= s->avctx->lowres;
  1409. const int block_s= 8>>lowres;
  1410. mb_x = s->mb_x;
  1411. mb_y = s->mb_y;
  1412. switch(s->mv_type) {
  1413. case MV_TYPE_16X16:
  1414. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1415. 0, 0, 0,
  1416. ref_picture, pix_op,
  1417. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
  1418. break;
  1419. case MV_TYPE_8X8:
  1420. mx = 0;
  1421. my = 0;
  1422. for(i=0;i<4;i++) {
  1423. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) * s->linesize)*block_s,
  1424. ref_picture[0], 0, 0,
  1425. (2*mb_x + (i & 1))*block_s, (2*mb_y + (i >>1))*block_s,
  1426. s->width, s->height, s->linesize,
  1427. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1428. block_s, block_s, pix_op,
  1429. s->mv[dir][i][0], s->mv[dir][i][1]);
  1430. mx += s->mv[dir][i][0];
  1431. my += s->mv[dir][i][1];
  1432. }
  1433. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  1434. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my);
  1435. break;
  1436. case MV_TYPE_FIELD:
  1437. if (s->picture_structure == PICT_FRAME) {
  1438. /* top field */
  1439. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1440. 1, 0, s->field_select[dir][0],
  1441. ref_picture, pix_op,
  1442. s->mv[dir][0][0], s->mv[dir][0][1], block_s);
  1443. /* bottom field */
  1444. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1445. 1, 1, s->field_select[dir][1],
  1446. ref_picture, pix_op,
  1447. s->mv[dir][1][0], s->mv[dir][1][1], block_s);
  1448. } else {
  1449. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != FF_B_TYPE && !s->first_field){
  1450. ref_picture= s->current_picture_ptr->data;
  1451. }
  1452. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1453. 0, 0, s->field_select[dir][0],
  1454. ref_picture, pix_op,
  1455. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
  1456. }
  1457. break;
  1458. case MV_TYPE_16X8:
  1459. for(i=0; i<2; i++){
  1460. uint8_t ** ref2picture;
  1461. if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == FF_B_TYPE || s->first_field){
  1462. ref2picture= ref_picture;
  1463. }else{
  1464. ref2picture= s->current_picture_ptr->data;
  1465. }
  1466. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1467. 0, 0, s->field_select[dir][i],
  1468. ref2picture, pix_op,
  1469. s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s);
  1470. dest_y += 2*block_s*s->linesize;
  1471. dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1472. dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1473. }
  1474. break;
  1475. case MV_TYPE_DMV:
  1476. if(s->picture_structure == PICT_FRAME){
  1477. for(i=0; i<2; i++){
  1478. int j;
  1479. for(j=0; j<2; j++){
  1480. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1481. 1, j, j^i,
  1482. ref_picture, pix_op,
  1483. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], block_s);
  1484. }
  1485. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1486. }
  1487. }else{
  1488. for(i=0; i<2; i++){
  1489. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1490. 0, 0, s->picture_structure != i+1,
  1491. ref_picture, pix_op,
  1492. s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s);
  1493. // after put we make avg of the same block
  1494. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1495. //opposite parity is always in the same frame if this is second field
  1496. if(!s->first_field){
  1497. ref_picture = s->current_picture_ptr->data;
  1498. }
  1499. }
  1500. }
  1501. break;
  1502. default: assert(0);
  1503. }
  1504. }
  1505. /* put block[] to dest[] */
  1506. static inline void put_dct(MpegEncContext *s,
  1507. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1508. {
  1509. s->dct_unquantize_intra(s, block, i, qscale);
  1510. s->dsp.idct_put (dest, line_size, block);
  1511. }
  1512. /* add block[] to dest[] */
  1513. static inline void add_dct(MpegEncContext *s,
  1514. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1515. {
  1516. if (s->block_last_index[i] >= 0) {
  1517. s->dsp.idct_add (dest, line_size, block);
  1518. }
  1519. }
  1520. static inline void add_dequant_dct(MpegEncContext *s,
  1521. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1522. {
  1523. if (s->block_last_index[i] >= 0) {
  1524. s->dct_unquantize_inter(s, block, i, qscale);
  1525. s->dsp.idct_add (dest, line_size, block);
  1526. }
  1527. }
  1528. /**
  1529. * cleans dc, ac, coded_block for the current non intra MB
  1530. */
  1531. void ff_clean_intra_table_entries(MpegEncContext *s)
  1532. {
  1533. int wrap = s->b8_stride;
  1534. int xy = s->block_index[0];
  1535. s->dc_val[0][xy ] =
  1536. s->dc_val[0][xy + 1 ] =
  1537. s->dc_val[0][xy + wrap] =
  1538. s->dc_val[0][xy + 1 + wrap] = 1024;
  1539. /* ac pred */
  1540. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1541. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1542. if (s->msmpeg4_version>=3) {
  1543. s->coded_block[xy ] =
  1544. s->coded_block[xy + 1 ] =
  1545. s->coded_block[xy + wrap] =
  1546. s->coded_block[xy + 1 + wrap] = 0;
  1547. }
  1548. /* chroma */
  1549. wrap = s->mb_stride;
  1550. xy = s->mb_x + s->mb_y * wrap;
  1551. s->dc_val[1][xy] =
  1552. s->dc_val[2][xy] = 1024;
  1553. /* ac pred */
  1554. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1555. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1556. s->mbintra_table[xy]= 0;
  1557. }
  1558. /* generic function called after a macroblock has been parsed by the
  1559. decoder or after it has been encoded by the encoder.
  1560. Important variables used:
  1561. s->mb_intra : true if intra macroblock
  1562. s->mv_dir : motion vector direction
  1563. s->mv_type : motion vector type
  1564. s->mv : motion vector
  1565. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1566. */
  1567. static av_always_inline
  1568. void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64],
  1569. int lowres_flag, int is_mpeg12)
  1570. {
  1571. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1572. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1573. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1574. return;
  1575. }
  1576. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1577. /* save DCT coefficients */
  1578. int i,j;
  1579. DCTELEM *dct = &s->current_picture.dct_coeff[mb_xy*64*6];
  1580. for(i=0; i<6; i++)
  1581. for(j=0; j<64; j++)
  1582. *dct++ = block[i][s->dsp.idct_permutation[j]];
  1583. }
  1584. s->current_picture.qscale_table[mb_xy]= s->qscale;
  1585. /* update DC predictors for P macroblocks */
  1586. if (!s->mb_intra) {
  1587. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1588. if(s->mbintra_table[mb_xy])
  1589. ff_clean_intra_table_entries(s);
  1590. } else {
  1591. s->last_dc[0] =
  1592. s->last_dc[1] =
  1593. s->last_dc[2] = 128 << s->intra_dc_precision;
  1594. }
  1595. }
  1596. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1597. s->mbintra_table[mb_xy]=1;
  1598. 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
  1599. uint8_t *dest_y, *dest_cb, *dest_cr;
  1600. int dct_linesize, dct_offset;
  1601. op_pixels_func (*op_pix)[4];
  1602. qpel_mc_func (*op_qpix)[16];
  1603. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
  1604. const int uvlinesize= s->current_picture.linesize[1];
  1605. const int readable= s->pict_type != FF_B_TYPE || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  1606. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  1607. /* avoid copy if macroblock skipped in last frame too */
  1608. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1609. if(!s->encoding){
  1610. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1611. const int age= s->current_picture.age;
  1612. assert(age);
  1613. if (s->mb_skipped) {
  1614. s->mb_skipped= 0;
  1615. assert(s->pict_type!=FF_I_TYPE);
  1616. (*mbskip_ptr) ++; /* indicate that this time we skipped it */
  1617. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1618. /* if previous was skipped too, then nothing to do ! */
  1619. if (*mbskip_ptr >= age && s->current_picture.reference){
  1620. return;
  1621. }
  1622. } else if(!s->current_picture.reference){
  1623. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  1624. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1625. } else{
  1626. *mbskip_ptr = 0; /* not skipped */
  1627. }
  1628. }
  1629. dct_linesize = linesize << s->interlaced_dct;
  1630. dct_offset =(s->interlaced_dct)? linesize : linesize*block_size;
  1631. if(readable){
  1632. dest_y= s->dest[0];
  1633. dest_cb= s->dest[1];
  1634. dest_cr= s->dest[2];
  1635. }else{
  1636. dest_y = s->b_scratchpad;
  1637. dest_cb= s->b_scratchpad+16*linesize;
  1638. dest_cr= s->b_scratchpad+32*linesize;
  1639. }
  1640. if (!s->mb_intra) {
  1641. /* motion handling */
  1642. /* decoding or more than one mb_type (MC was already done otherwise) */
  1643. if(!s->encoding){
  1644. if(lowres_flag){
  1645. h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
  1646. if (s->mv_dir & MV_DIR_FORWARD) {
  1647. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix);
  1648. op_pix = s->dsp.avg_h264_chroma_pixels_tab;
  1649. }
  1650. if (s->mv_dir & MV_DIR_BACKWARD) {
  1651. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix);
  1652. }
  1653. }else{
  1654. op_qpix= s->me.qpel_put;
  1655. if ((!s->no_rounding) || s->pict_type==FF_B_TYPE){
  1656. op_pix = s->dsp.put_pixels_tab;
  1657. }else{
  1658. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1659. }
  1660. if (s->mv_dir & MV_DIR_FORWARD) {
  1661. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  1662. op_pix = s->dsp.avg_pixels_tab;
  1663. op_qpix= s->me.qpel_avg;
  1664. }
  1665. if (s->mv_dir & MV_DIR_BACKWARD) {
  1666. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  1667. }
  1668. }
  1669. }
  1670. /* skip dequant / idct if we are really late ;) */
  1671. if(s->hurry_up>1) goto skip_idct;
  1672. if(s->avctx->skip_idct){
  1673. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == FF_B_TYPE)
  1674. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != FF_I_TYPE)
  1675. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1676. goto skip_idct;
  1677. }
  1678. /* add dct residue */
  1679. if(s->encoding || !( s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  1680. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1681. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1682. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1683. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1684. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1685. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1686. if (s->chroma_y_shift){
  1687. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1688. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1689. }else{
  1690. dct_linesize >>= 1;
  1691. dct_offset >>=1;
  1692. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1693. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1694. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1695. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1696. }
  1697. }
  1698. } else if(is_mpeg12 || (s->codec_id != CODEC_ID_WMV2)){
  1699. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1700. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1701. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1702. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1703. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1704. if(s->chroma_y_shift){//Chroma420
  1705. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1706. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1707. }else{
  1708. //chroma422
  1709. dct_linesize = uvlinesize << s->interlaced_dct;
  1710. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  1711. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1712. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1713. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1714. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1715. if(!s->chroma_x_shift){//Chroma444
  1716. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  1717. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  1718. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  1719. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  1720. }
  1721. }
  1722. }//fi gray
  1723. }
  1724. else if (CONFIG_WMV2) {
  1725. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1726. }
  1727. } else {
  1728. /* dct only in intra block */
  1729. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  1730. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1731. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1732. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1733. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1734. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1735. if(s->chroma_y_shift){
  1736. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1737. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1738. }else{
  1739. dct_offset >>=1;
  1740. dct_linesize >>=1;
  1741. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1742. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1743. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1744. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1745. }
  1746. }
  1747. }else{
  1748. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1749. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1750. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1751. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1752. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1753. if(s->chroma_y_shift){
  1754. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1755. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1756. }else{
  1757. dct_linesize = uvlinesize << s->interlaced_dct;
  1758. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  1759. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  1760. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  1761. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1762. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1763. if(!s->chroma_x_shift){//Chroma444
  1764. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  1765. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  1766. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  1767. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  1768. }
  1769. }
  1770. }//gray
  1771. }
  1772. }
  1773. skip_idct:
  1774. if(!readable){
  1775. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1776. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1777. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1778. }
  1779. }
  1780. }
  1781. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
  1782. #if !CONFIG_SMALL
  1783. if(s->out_format == FMT_MPEG1) {
  1784. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  1785. else MPV_decode_mb_internal(s, block, 0, 1);
  1786. } else
  1787. #endif
  1788. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  1789. else MPV_decode_mb_internal(s, block, 0, 0);
  1790. }
  1791. /**
  1792. *
  1793. * @param h is the normal height, this will be reduced automatically if needed for the last row
  1794. */
  1795. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  1796. if (s->avctx->draw_horiz_band) {
  1797. AVFrame *src;
  1798. int offset[4];
  1799. if(s->picture_structure != PICT_FRAME){
  1800. h <<= 1;
  1801. y <<= 1;
  1802. if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  1803. }
  1804. h= FFMIN(h, s->avctx->height - y);
  1805. if(s->pict_type==FF_B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  1806. src= (AVFrame*)s->current_picture_ptr;
  1807. else if(s->last_picture_ptr)
  1808. src= (AVFrame*)s->last_picture_ptr;
  1809. else
  1810. return;
  1811. if(s->pict_type==FF_B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  1812. offset[0]=
  1813. offset[1]=
  1814. offset[2]=
  1815. offset[3]= 0;
  1816. }else{
  1817. offset[0]= y * s->linesize;
  1818. offset[1]=
  1819. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  1820. offset[3]= 0;
  1821. }
  1822. emms_c();
  1823. s->avctx->draw_horiz_band(s->avctx, src, offset,
  1824. y, s->picture_structure, h);
  1825. }
  1826. }
  1827. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  1828. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
  1829. const int uvlinesize= s->current_picture.linesize[1];
  1830. const int mb_size= 4 - s->avctx->lowres;
  1831. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  1832. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  1833. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  1834. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  1835. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  1836. 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;
  1837. //block_index is not used by mpeg2, so it is not affected by chroma_format
  1838. s->dest[0] = s->current_picture.data[0] + ((s->mb_x - 1) << mb_size);
  1839. s->dest[1] = s->current_picture.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  1840. s->dest[2] = s->current_picture.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  1841. if(!(s->pict_type==FF_B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  1842. {
  1843. s->dest[0] += s->mb_y * linesize << mb_size;
  1844. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1845. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1846. }
  1847. }
  1848. void ff_mpeg_flush(AVCodecContext *avctx){
  1849. int i;
  1850. MpegEncContext *s = avctx->priv_data;
  1851. if(s==NULL || s->picture==NULL)
  1852. return;
  1853. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1854. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  1855. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  1856. free_frame_buffer(s, &s->picture[i]);
  1857. }
  1858. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  1859. s->mb_x= s->mb_y= 0;
  1860. s->closed_gop= 0;
  1861. s->parse_context.state= -1;
  1862. s->parse_context.frame_start_found= 0;
  1863. s->parse_context.overread= 0;
  1864. s->parse_context.overread_index= 0;
  1865. s->parse_context.index= 0;
  1866. s->parse_context.last_index= 0;
  1867. s->bitstream_buffer_size=0;
  1868. s->pp_time=0;
  1869. }
  1870. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  1871. DCTELEM *block, int n, int qscale)
  1872. {
  1873. int i, level, nCoeffs;
  1874. const uint16_t *quant_matrix;
  1875. nCoeffs= s->block_last_index[n];
  1876. if (n < 4)
  1877. block[0] = block[0] * s->y_dc_scale;
  1878. else
  1879. block[0] = block[0] * s->c_dc_scale;
  1880. /* XXX: only mpeg1 */
  1881. quant_matrix = s->intra_matrix;
  1882. for(i=1;i<=nCoeffs;i++) {
  1883. int j= s->intra_scantable.permutated[i];
  1884. level = block[j];
  1885. if (level) {
  1886. if (level < 0) {
  1887. level = -level;
  1888. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1889. level = (level - 1) | 1;
  1890. level = -level;
  1891. } else {
  1892. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1893. level = (level - 1) | 1;
  1894. }
  1895. block[j] = level;
  1896. }
  1897. }
  1898. }
  1899. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  1900. DCTELEM *block, int n, int qscale)
  1901. {
  1902. int i, level, nCoeffs;
  1903. const uint16_t *quant_matrix;
  1904. nCoeffs= s->block_last_index[n];
  1905. quant_matrix = s->inter_matrix;
  1906. for(i=0; i<=nCoeffs; i++) {
  1907. int j= s->intra_scantable.permutated[i];
  1908. level = block[j];
  1909. if (level) {
  1910. if (level < 0) {
  1911. level = -level;
  1912. level = (((level << 1) + 1) * qscale *
  1913. ((int) (quant_matrix[j]))) >> 4;
  1914. level = (level - 1) | 1;
  1915. level = -level;
  1916. } else {
  1917. level = (((level << 1) + 1) * qscale *
  1918. ((int) (quant_matrix[j]))) >> 4;
  1919. level = (level - 1) | 1;
  1920. }
  1921. block[j] = level;
  1922. }
  1923. }
  1924. }
  1925. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  1926. DCTELEM *block, int n, int qscale)
  1927. {
  1928. int i, level, nCoeffs;
  1929. const uint16_t *quant_matrix;
  1930. if(s->alternate_scan) nCoeffs= 63;
  1931. else nCoeffs= s->block_last_index[n];
  1932. if (n < 4)
  1933. block[0] = block[0] * s->y_dc_scale;
  1934. else
  1935. block[0] = block[0] * s->c_dc_scale;
  1936. quant_matrix = s->intra_matrix;
  1937. for(i=1;i<=nCoeffs;i++) {
  1938. int j= s->intra_scantable.permutated[i];
  1939. level = block[j];
  1940. if (level) {
  1941. if (level < 0) {
  1942. level = -level;
  1943. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1944. level = -level;
  1945. } else {
  1946. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1947. }
  1948. block[j] = level;
  1949. }
  1950. }
  1951. }
  1952. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  1953. DCTELEM *block, int n, int qscale)
  1954. {
  1955. int i, level, nCoeffs;
  1956. const uint16_t *quant_matrix;
  1957. int sum=-1;
  1958. if(s->alternate_scan) nCoeffs= 63;
  1959. else nCoeffs= s->block_last_index[n];
  1960. if (n < 4)
  1961. block[0] = block[0] * s->y_dc_scale;
  1962. else
  1963. block[0] = block[0] * s->c_dc_scale;
  1964. quant_matrix = s->intra_matrix;
  1965. for(i=1;i<=nCoeffs;i++) {
  1966. int j= s->intra_scantable.permutated[i];
  1967. level = block[j];
  1968. if (level) {
  1969. if (level < 0) {
  1970. level = -level;
  1971. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1972. level = -level;
  1973. } else {
  1974. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1975. }
  1976. block[j] = level;
  1977. sum+=level;
  1978. }
  1979. }
  1980. block[63]^=sum&1;
  1981. }
  1982. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  1983. DCTELEM *block, int n, int qscale)
  1984. {
  1985. int i, level, nCoeffs;
  1986. const uint16_t *quant_matrix;
  1987. int sum=-1;
  1988. if(s->alternate_scan) nCoeffs= 63;
  1989. else nCoeffs= s->block_last_index[n];
  1990. quant_matrix = s->inter_matrix;
  1991. for(i=0; i<=nCoeffs; i++) {
  1992. int j= s->intra_scantable.permutated[i];
  1993. level = block[j];
  1994. if (level) {
  1995. if (level < 0) {
  1996. level = -level;
  1997. level = (((level << 1) + 1) * qscale *
  1998. ((int) (quant_matrix[j]))) >> 4;
  1999. level = -level;
  2000. } else {
  2001. level = (((level << 1) + 1) * qscale *
  2002. ((int) (quant_matrix[j]))) >> 4;
  2003. }
  2004. block[j] = level;
  2005. sum+=level;
  2006. }
  2007. }
  2008. block[63]^=sum&1;
  2009. }
  2010. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2011. DCTELEM *block, int n, int qscale)
  2012. {
  2013. int i, level, qmul, qadd;
  2014. int nCoeffs;
  2015. assert(s->block_last_index[n]>=0);
  2016. qmul = qscale << 1;
  2017. if (!s->h263_aic) {
  2018. if (n < 4)
  2019. block[0] = block[0] * s->y_dc_scale;
  2020. else
  2021. block[0] = block[0] * s->c_dc_scale;
  2022. qadd = (qscale - 1) | 1;
  2023. }else{
  2024. qadd = 0;
  2025. }
  2026. if(s->ac_pred)
  2027. nCoeffs=63;
  2028. else
  2029. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2030. for(i=1; i<=nCoeffs; i++) {
  2031. level = block[i];
  2032. if (level) {
  2033. if (level < 0) {
  2034. level = level * qmul - qadd;
  2035. } else {
  2036. level = level * qmul + qadd;
  2037. }
  2038. block[i] = level;
  2039. }
  2040. }
  2041. }
  2042. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2043. DCTELEM *block, int n, int qscale)
  2044. {
  2045. int i, level, qmul, qadd;
  2046. int nCoeffs;
  2047. assert(s->block_last_index[n]>=0);
  2048. qadd = (qscale - 1) | 1;
  2049. qmul = qscale << 1;
  2050. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2051. for(i=0; i<=nCoeffs; i++) {
  2052. level = block[i];
  2053. if (level) {
  2054. if (level < 0) {
  2055. level = level * qmul - qadd;
  2056. } else {
  2057. level = level * qmul + qadd;
  2058. }
  2059. block[i] = level;
  2060. }
  2061. }
  2062. }
  2063. /**
  2064. * set qscale and update qscale dependent variables.
  2065. */
  2066. void ff_set_qscale(MpegEncContext * s, int qscale)
  2067. {
  2068. if (qscale < 1)
  2069. qscale = 1;
  2070. else if (qscale > 31)
  2071. qscale = 31;
  2072. s->qscale = qscale;
  2073. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2074. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2075. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2076. }