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.

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