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.

2413 lines
89KB

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