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.

2422 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. int ff_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(HAVE_ALTIVEC)
  132. MPV_common_init_altivec(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, threads;
  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. ff_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. /* h264 does thread context setup itself, but it needs context[0]
  504. * to be fully initialized for the error resilience code */
  505. threads = s->codec_id == CODEC_ID_H264 ? 1 : s->avctx->thread_count;
  506. for(i=1; i<threads; i++){
  507. s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
  508. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  509. }
  510. for(i=0; i<threads; i++){
  511. if(init_duplicate_context(s->thread_context[i], s) < 0)
  512. goto fail;
  513. s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count;
  514. s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
  515. }
  516. return 0;
  517. fail:
  518. MPV_common_end(s);
  519. return -1;
  520. }
  521. /* init common structure for both encoder and decoder */
  522. void MPV_common_end(MpegEncContext *s)
  523. {
  524. int i, j, k;
  525. for(i=0; i<s->avctx->thread_count; i++){
  526. free_duplicate_context(s->thread_context[i]);
  527. }
  528. for(i=1; i<s->avctx->thread_count; i++){
  529. av_freep(&s->thread_context[i]);
  530. }
  531. av_freep(&s->parse_context.buffer);
  532. s->parse_context.buffer_size=0;
  533. av_freep(&s->mb_type);
  534. av_freep(&s->p_mv_table_base);
  535. av_freep(&s->b_forw_mv_table_base);
  536. av_freep(&s->b_back_mv_table_base);
  537. av_freep(&s->b_bidir_forw_mv_table_base);
  538. av_freep(&s->b_bidir_back_mv_table_base);
  539. av_freep(&s->b_direct_mv_table_base);
  540. s->p_mv_table= NULL;
  541. s->b_forw_mv_table= NULL;
  542. s->b_back_mv_table= NULL;
  543. s->b_bidir_forw_mv_table= NULL;
  544. s->b_bidir_back_mv_table= NULL;
  545. s->b_direct_mv_table= NULL;
  546. for(i=0; i<2; i++){
  547. for(j=0; j<2; j++){
  548. for(k=0; k<2; k++){
  549. av_freep(&s->b_field_mv_table_base[i][j][k]);
  550. s->b_field_mv_table[i][j][k]=NULL;
  551. }
  552. av_freep(&s->b_field_select_table[i][j]);
  553. av_freep(&s->p_field_mv_table_base[i][j]);
  554. s->p_field_mv_table[i][j]=NULL;
  555. }
  556. av_freep(&s->p_field_select_table[i]);
  557. }
  558. av_freep(&s->dc_val_base);
  559. av_freep(&s->ac_val_base);
  560. av_freep(&s->coded_block_base);
  561. av_freep(&s->mbintra_table);
  562. av_freep(&s->cbp_table);
  563. av_freep(&s->pred_dir_table);
  564. av_freep(&s->mbskip_table);
  565. av_freep(&s->prev_pict_types);
  566. av_freep(&s->bitstream_buffer);
  567. s->allocated_bitstream_buffer_size=0;
  568. av_freep(&s->avctx->stats_out);
  569. av_freep(&s->ac_stats);
  570. av_freep(&s->error_status_table);
  571. av_freep(&s->mb_index2xy);
  572. av_freep(&s->lambda_table);
  573. av_freep(&s->q_intra_matrix);
  574. av_freep(&s->q_inter_matrix);
  575. av_freep(&s->q_intra_matrix16);
  576. av_freep(&s->q_inter_matrix16);
  577. av_freep(&s->input_picture);
  578. av_freep(&s->reordered_input_picture);
  579. av_freep(&s->dct_offset);
  580. if(s->picture){
  581. for(i=0; i<MAX_PICTURE_COUNT; i++){
  582. free_picture(s, &s->picture[i]);
  583. }
  584. }
  585. av_freep(&s->picture);
  586. s->context_initialized = 0;
  587. s->last_picture_ptr=
  588. s->next_picture_ptr=
  589. s->current_picture_ptr= NULL;
  590. s->linesize= s->uvlinesize= 0;
  591. for(i=0; i<3; i++)
  592. av_freep(&s->visualization_buffer[i]);
  593. avcodec_default_free_buffers(s->avctx);
  594. }
  595. void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3])
  596. {
  597. int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
  598. uint8_t index_run[MAX_RUN+1];
  599. int last, run, level, start, end, i;
  600. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  601. if(static_store && rl->max_level[0])
  602. return;
  603. /* compute max_level[], max_run[] and index_run[] */
  604. for(last=0;last<2;last++) {
  605. if (last == 0) {
  606. start = 0;
  607. end = rl->last;
  608. } else {
  609. start = rl->last;
  610. end = rl->n;
  611. }
  612. memset(max_level, 0, MAX_RUN + 1);
  613. memset(max_run, 0, MAX_LEVEL + 1);
  614. memset(index_run, rl->n, MAX_RUN + 1);
  615. for(i=start;i<end;i++) {
  616. run = rl->table_run[i];
  617. level = rl->table_level[i];
  618. if (index_run[run] == rl->n)
  619. index_run[run] = i;
  620. if (level > max_level[run])
  621. max_level[run] = level;
  622. if (run > max_run[level])
  623. max_run[level] = run;
  624. }
  625. if(static_store)
  626. rl->max_level[last] = static_store[last];
  627. else
  628. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  629. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  630. if(static_store)
  631. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  632. else
  633. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  634. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  635. if(static_store)
  636. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  637. else
  638. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  639. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  640. }
  641. }
  642. void init_vlc_rl(RLTable *rl, int use_static)
  643. {
  644. int i, q;
  645. /* Return if static table is already initialized */
  646. if(use_static && rl->rl_vlc[0])
  647. return;
  648. init_vlc(&rl->vlc, 9, rl->n + 1,
  649. &rl->table_vlc[0][1], 4, 2,
  650. &rl->table_vlc[0][0], 4, 2, use_static);
  651. for(q=0; q<32; q++){
  652. int qmul= q*2;
  653. int qadd= (q-1)|1;
  654. if(q==0){
  655. qmul=1;
  656. qadd=0;
  657. }
  658. if(use_static)
  659. rl->rl_vlc[q]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
  660. else
  661. rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
  662. for(i=0; i<rl->vlc.table_size; i++){
  663. int code= rl->vlc.table[i][0];
  664. int len = rl->vlc.table[i][1];
  665. int level, run;
  666. if(len==0){ // illegal code
  667. run= 66;
  668. level= MAX_LEVEL;
  669. }else if(len<0){ //more bits needed
  670. run= 0;
  671. level= code;
  672. }else{
  673. if(code==rl->n){ //esc
  674. run= 66;
  675. level= 0;
  676. }else{
  677. run= rl->table_run [code] + 1;
  678. level= rl->table_level[code] * qmul + qadd;
  679. if(code >= rl->last) run+=192;
  680. }
  681. }
  682. rl->rl_vlc[q][i].len= len;
  683. rl->rl_vlc[q][i].level= level;
  684. rl->rl_vlc[q][i].run= run;
  685. }
  686. }
  687. }
  688. /* draw the edges of width 'w' of an image of size width, height */
  689. //FIXME check that this is ok for mpeg4 interlaced
  690. static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
  691. {
  692. uint8_t *ptr, *last_line;
  693. int i;
  694. last_line = buf + (height - 1) * wrap;
  695. for(i=0;i<w;i++) {
  696. /* top and bottom */
  697. memcpy(buf - (i + 1) * wrap, buf, width);
  698. memcpy(last_line + (i + 1) * wrap, last_line, width);
  699. }
  700. /* left and right */
  701. ptr = buf;
  702. for(i=0;i<height;i++) {
  703. memset(ptr - w, ptr[0], w);
  704. memset(ptr + width, ptr[width-1], w);
  705. ptr += wrap;
  706. }
  707. /* corners */
  708. for(i=0;i<w;i++) {
  709. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  710. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  711. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  712. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  713. }
  714. }
  715. int ff_find_unused_picture(MpegEncContext *s, int shared){
  716. int i;
  717. if(shared){
  718. for(i=0; i<MAX_PICTURE_COUNT; i++){
  719. if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
  720. }
  721. }else{
  722. for(i=0; i<MAX_PICTURE_COUNT; i++){
  723. if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
  724. }
  725. for(i=0; i<MAX_PICTURE_COUNT; i++){
  726. if(s->picture[i].data[0]==NULL) return i;
  727. }
  728. }
  729. assert(0);
  730. return -1;
  731. }
  732. static void update_noise_reduction(MpegEncContext *s){
  733. int intra, i;
  734. for(intra=0; intra<2; intra++){
  735. if(s->dct_count[intra] > (1<<16)){
  736. for(i=0; i<64; i++){
  737. s->dct_error_sum[intra][i] >>=1;
  738. }
  739. s->dct_count[intra] >>= 1;
  740. }
  741. for(i=0; i<64; i++){
  742. 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);
  743. }
  744. }
  745. }
  746. /**
  747. * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
  748. */
  749. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  750. {
  751. int i;
  752. AVFrame *pic;
  753. s->mb_skipped = 0;
  754. assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
  755. /* mark&release old frames */
  756. if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr->data[0]) {
  757. if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
  758. avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
  759. /* release forgotten pictures */
  760. /* if(mpeg124/h263) */
  761. if(!s->encoding){
  762. for(i=0; i<MAX_PICTURE_COUNT; i++){
  763. if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
  764. av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
  765. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  766. }
  767. }
  768. }
  769. }
  770. }
  771. alloc:
  772. if(!s->encoding){
  773. /* release non reference frames */
  774. for(i=0; i<MAX_PICTURE_COUNT; i++){
  775. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  776. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  777. }
  778. }
  779. if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
  780. pic= (AVFrame*)s->current_picture_ptr; //we allready have a unused image (maybe it was set before reading the header)
  781. else{
  782. i= ff_find_unused_picture(s, 0);
  783. pic= (AVFrame*)&s->picture[i];
  784. }
  785. pic->reference= 0;
  786. if (!s->dropable){
  787. if (s->codec_id == CODEC_ID_H264)
  788. pic->reference = s->picture_structure;
  789. else if (s->pict_type != B_TYPE)
  790. pic->reference = 3;
  791. }
  792. pic->coded_picture_number= s->coded_picture_number++;
  793. if( alloc_picture(s, (Picture*)pic, 0) < 0)
  794. return -1;
  795. s->current_picture_ptr= (Picture*)pic;
  796. s->current_picture_ptr->top_field_first= s->top_field_first; //FIXME use only the vars from current_pic
  797. s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
  798. }
  799. s->current_picture_ptr->pict_type= s->pict_type;
  800. // if(s->flags && CODEC_FLAG_QSCALE)
  801. // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
  802. s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
  803. copy_picture(&s->current_picture, s->current_picture_ptr);
  804. if (s->pict_type != B_TYPE) {
  805. s->last_picture_ptr= s->next_picture_ptr;
  806. if(!s->dropable)
  807. s->next_picture_ptr= s->current_picture_ptr;
  808. }
  809. /* 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,
  810. s->last_picture_ptr ? s->last_picture_ptr->data[0] : NULL,
  811. s->next_picture_ptr ? s->next_picture_ptr->data[0] : NULL,
  812. s->current_picture_ptr ? s->current_picture_ptr->data[0] : NULL,
  813. s->pict_type, s->dropable);*/
  814. if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
  815. if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
  816. if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL) && !s->dropable){
  817. av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
  818. assert(s->pict_type != B_TYPE); //these should have been dropped if we don't have a reference
  819. goto alloc;
  820. }
  821. assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
  822. if(s->picture_structure!=PICT_FRAME && s->out_format != FMT_H264){
  823. int i;
  824. for(i=0; i<4; i++){
  825. if(s->picture_structure == PICT_BOTTOM_FIELD){
  826. s->current_picture.data[i] += s->current_picture.linesize[i];
  827. }
  828. s->current_picture.linesize[i] *= 2;
  829. s->last_picture.linesize[i] *=2;
  830. s->next_picture.linesize[i] *=2;
  831. }
  832. }
  833. s->hurry_up= s->avctx->hurry_up;
  834. s->error_resilience= avctx->error_resilience;
  835. /* set dequantizer, we can't do it during init as it might change for mpeg4
  836. and we can't do it in the header decode as init is not called for mpeg4 there yet */
  837. if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
  838. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  839. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  840. }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  841. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  842. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  843. }else{
  844. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  845. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  846. }
  847. if(s->dct_error_sum){
  848. assert(s->avctx->noise_reduction && s->encoding);
  849. update_noise_reduction(s);
  850. }
  851. #ifdef HAVE_XVMC
  852. if(s->avctx->xvmc_acceleration)
  853. return XVMC_field_start(s, avctx);
  854. #endif
  855. return 0;
  856. }
  857. /* generic function for encode/decode called after a frame has been coded/decoded */
  858. void MPV_frame_end(MpegEncContext *s)
  859. {
  860. int i;
  861. /* draw edge for correct motion prediction if outside */
  862. #ifdef HAVE_XVMC
  863. //just to make sure that all data is rendered.
  864. if(s->avctx->xvmc_acceleration){
  865. XVMC_field_end(s);
  866. }else
  867. #endif
  868. if(s->unrestricted_mv && s->current_picture.reference && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  869. draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
  870. draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  871. draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  872. }
  873. emms_c();
  874. s->last_pict_type = s->pict_type;
  875. s->last_lambda_for[s->pict_type]= s->current_picture_ptr->quality;
  876. if(s->pict_type!=B_TYPE){
  877. s->last_non_b_pict_type= s->pict_type;
  878. }
  879. #if 0
  880. /* copy back current_picture variables */
  881. for(i=0; i<MAX_PICTURE_COUNT; i++){
  882. if(s->picture[i].data[0] == s->current_picture.data[0]){
  883. s->picture[i]= s->current_picture;
  884. break;
  885. }
  886. }
  887. assert(i<MAX_PICTURE_COUNT);
  888. #endif
  889. if(s->encoding){
  890. /* release non-reference frames */
  891. for(i=0; i<MAX_PICTURE_COUNT; i++){
  892. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  893. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  894. }
  895. }
  896. }
  897. // clear copies, to avoid confusion
  898. #if 0
  899. memset(&s->last_picture, 0, sizeof(Picture));
  900. memset(&s->next_picture, 0, sizeof(Picture));
  901. memset(&s->current_picture, 0, sizeof(Picture));
  902. #endif
  903. s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
  904. }
  905. /**
  906. * draws an line from (ex, ey) -> (sx, sy).
  907. * @param w width of the image
  908. * @param h height of the image
  909. * @param stride stride/linesize of the image
  910. * @param color color of the arrow
  911. */
  912. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  913. int x, y, fr, f;
  914. sx= av_clip(sx, 0, w-1);
  915. sy= av_clip(sy, 0, h-1);
  916. ex= av_clip(ex, 0, w-1);
  917. ey= av_clip(ey, 0, h-1);
  918. buf[sy*stride + sx]+= color;
  919. if(FFABS(ex - sx) > FFABS(ey - sy)){
  920. if(sx > ex){
  921. FFSWAP(int, sx, ex);
  922. FFSWAP(int, sy, ey);
  923. }
  924. buf+= sx + sy*stride;
  925. ex-= sx;
  926. f= ((ey-sy)<<16)/ex;
  927. for(x= 0; x <= ex; x++){
  928. y = (x*f)>>16;
  929. fr= (x*f)&0xFFFF;
  930. buf[ y *stride + x]+= (color*(0x10000-fr))>>16;
  931. buf[(y+1)*stride + x]+= (color* fr )>>16;
  932. }
  933. }else{
  934. if(sy > ey){
  935. FFSWAP(int, sx, ex);
  936. FFSWAP(int, sy, ey);
  937. }
  938. buf+= sx + sy*stride;
  939. ey-= sy;
  940. if(ey) f= ((ex-sx)<<16)/ey;
  941. else f= 0;
  942. for(y= 0; y <= ey; y++){
  943. x = (y*f)>>16;
  944. fr= (y*f)&0xFFFF;
  945. buf[y*stride + x ]+= (color*(0x10000-fr))>>16;;
  946. buf[y*stride + x+1]+= (color* fr )>>16;;
  947. }
  948. }
  949. }
  950. /**
  951. * draws an arrow from (ex, ey) -> (sx, sy).
  952. * @param w width of the image
  953. * @param h height of the image
  954. * @param stride stride/linesize of the image
  955. * @param color color of the arrow
  956. */
  957. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  958. int dx,dy;
  959. sx= av_clip(sx, -100, w+100);
  960. sy= av_clip(sy, -100, h+100);
  961. ex= av_clip(ex, -100, w+100);
  962. ey= av_clip(ey, -100, h+100);
  963. dx= ex - sx;
  964. dy= ey - sy;
  965. if(dx*dx + dy*dy > 3*3){
  966. int rx= dx + dy;
  967. int ry= -dx + dy;
  968. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  969. //FIXME subpixel accuracy
  970. rx= ROUNDED_DIV(rx*3<<4, length);
  971. ry= ROUNDED_DIV(ry*3<<4, length);
  972. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  973. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  974. }
  975. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  976. }
  977. /**
  978. * prints debuging info for the given picture.
  979. */
  980. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
  981. if(!pict || !pict->mb_type) return;
  982. if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
  983. int x,y;
  984. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  985. switch (pict->pict_type) {
  986. case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
  987. case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
  988. case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
  989. case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
  990. case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
  991. case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;
  992. }
  993. for(y=0; y<s->mb_height; y++){
  994. for(x=0; x<s->mb_width; x++){
  995. if(s->avctx->debug&FF_DEBUG_SKIP){
  996. int count= s->mbskip_table[x + y*s->mb_stride];
  997. if(count>9) count=9;
  998. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  999. }
  1000. if(s->avctx->debug&FF_DEBUG_QP){
  1001. av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
  1002. }
  1003. if(s->avctx->debug&FF_DEBUG_MB_TYPE){
  1004. int mb_type= pict->mb_type[x + y*s->mb_stride];
  1005. //Type & MV direction
  1006. if(IS_PCM(mb_type))
  1007. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1008. else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1009. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1010. else if(IS_INTRA4x4(mb_type))
  1011. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1012. else if(IS_INTRA16x16(mb_type))
  1013. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1014. else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1015. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1016. else if(IS_DIRECT(mb_type))
  1017. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1018. else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
  1019. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1020. else if(IS_GMC(mb_type))
  1021. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1022. else if(IS_SKIP(mb_type))
  1023. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1024. else if(!USES_LIST(mb_type, 1))
  1025. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1026. else if(!USES_LIST(mb_type, 0))
  1027. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1028. else{
  1029. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1030. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1031. }
  1032. //segmentation
  1033. if(IS_8X8(mb_type))
  1034. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1035. else if(IS_16X8(mb_type))
  1036. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1037. else if(IS_8X16(mb_type))
  1038. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1039. else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
  1040. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1041. else
  1042. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1043. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
  1044. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1045. else
  1046. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1047. }
  1048. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1049. }
  1050. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1051. }
  1052. }
  1053. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  1054. const int shift= 1 + s->quarter_sample;
  1055. int mb_y;
  1056. uint8_t *ptr;
  1057. int i;
  1058. int h_chroma_shift, v_chroma_shift;
  1059. const int width = s->avctx->width;
  1060. const int height= s->avctx->height;
  1061. const int mv_sample_log2= 4 - pict->motion_subsample_log2;
  1062. const int mv_stride= (s->mb_width << mv_sample_log2) + (s->codec_id == CODEC_ID_H264 ? 0 : 1);
  1063. s->low_delay=0; //needed to see the vectors without trashing the buffers
  1064. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1065. for(i=0; i<3; i++){
  1066. memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*height:pict->linesize[i]*height >> v_chroma_shift);
  1067. pict->data[i]= s->visualization_buffer[i];
  1068. }
  1069. pict->type= FF_BUFFER_TYPE_COPY;
  1070. ptr= pict->data[0];
  1071. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  1072. int mb_x;
  1073. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  1074. const int mb_index= mb_x + mb_y*s->mb_stride;
  1075. if((s->avctx->debug_mv) && pict->motion_val){
  1076. int type;
  1077. for(type=0; type<3; type++){
  1078. int direction = 0;
  1079. switch (type) {
  1080. case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=FF_P_TYPE))
  1081. continue;
  1082. direction = 0;
  1083. break;
  1084. case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=FF_B_TYPE))
  1085. continue;
  1086. direction = 0;
  1087. break;
  1088. case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=FF_B_TYPE))
  1089. continue;
  1090. direction = 1;
  1091. break;
  1092. }
  1093. if(!USES_LIST(pict->mb_type[mb_index], direction))
  1094. continue;
  1095. if(IS_8X8(pict->mb_type[mb_index])){
  1096. int i;
  1097. for(i=0; i<4; i++){
  1098. int sx= mb_x*16 + 4 + 8*(i&1);
  1099. int sy= mb_y*16 + 4 + 8*(i>>1);
  1100. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1101. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1102. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1103. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1104. }
  1105. }else if(IS_16X8(pict->mb_type[mb_index])){
  1106. int i;
  1107. for(i=0; i<2; i++){
  1108. int sx=mb_x*16 + 8;
  1109. int sy=mb_y*16 + 4 + 8*i;
  1110. int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << (mv_sample_log2-1);
  1111. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1112. int my=(pict->motion_val[direction][xy][1]>>shift);
  1113. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1114. my*=2;
  1115. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1116. }
  1117. }else if(IS_8X16(pict->mb_type[mb_index])){
  1118. int i;
  1119. for(i=0; i<2; i++){
  1120. int sx=mb_x*16 + 4 + 8*i;
  1121. int sy=mb_y*16 + 8;
  1122. int xy= (mb_x*2 + i + mb_y*2*mv_stride) << (mv_sample_log2-1);
  1123. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1124. int my=(pict->motion_val[direction][xy][1]>>shift);
  1125. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1126. my*=2;
  1127. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1128. }
  1129. }else{
  1130. int sx= mb_x*16 + 8;
  1131. int sy= mb_y*16 + 8;
  1132. int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2;
  1133. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1134. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1135. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1136. }
  1137. }
  1138. }
  1139. if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
  1140. uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
  1141. int y;
  1142. for(y=0; y<8; y++){
  1143. *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= c;
  1144. *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= c;
  1145. }
  1146. }
  1147. if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
  1148. int mb_type= pict->mb_type[mb_index];
  1149. uint64_t u,v;
  1150. int y;
  1151. #define COLOR(theta, r)\
  1152. u= (int)(128 + r*cos(theta*3.141592/180));\
  1153. v= (int)(128 + r*sin(theta*3.141592/180));
  1154. u=v=128;
  1155. if(IS_PCM(mb_type)){
  1156. COLOR(120,48)
  1157. }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
  1158. COLOR(30,48)
  1159. }else if(IS_INTRA4x4(mb_type)){
  1160. COLOR(90,48)
  1161. }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
  1162. // COLOR(120,48)
  1163. }else if(IS_DIRECT(mb_type)){
  1164. COLOR(150,48)
  1165. }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
  1166. COLOR(170,48)
  1167. }else if(IS_GMC(mb_type)){
  1168. COLOR(190,48)
  1169. }else if(IS_SKIP(mb_type)){
  1170. // COLOR(180,48)
  1171. }else if(!USES_LIST(mb_type, 1)){
  1172. COLOR(240,48)
  1173. }else if(!USES_LIST(mb_type, 0)){
  1174. COLOR(0,48)
  1175. }else{
  1176. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1177. COLOR(300,48)
  1178. }
  1179. u*= 0x0101010101010101ULL;
  1180. v*= 0x0101010101010101ULL;
  1181. for(y=0; y<8; y++){
  1182. *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= u;
  1183. *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= v;
  1184. }
  1185. //segmentation
  1186. if(IS_8X8(mb_type) || IS_16X8(mb_type)){
  1187. *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1188. *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1189. }
  1190. if(IS_8X8(mb_type) || IS_8X16(mb_type)){
  1191. for(y=0; y<16; y++)
  1192. pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
  1193. }
  1194. if(IS_8X8(mb_type) && mv_sample_log2 >= 2){
  1195. int dm= 1 << (mv_sample_log2-2);
  1196. for(i=0; i<4; i++){
  1197. int sx= mb_x*16 + 8*(i&1);
  1198. int sy= mb_y*16 + 8*(i>>1);
  1199. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1200. //FIXME bidir
  1201. int32_t *mv = (int32_t*)&pict->motion_val[0][xy];
  1202. if(mv[0] != mv[dm] || mv[dm*mv_stride] != mv[dm*(mv_stride+1)])
  1203. for(y=0; y<8; y++)
  1204. pict->data[0][sx + 4 + (sy + y)*pict->linesize[0]]^= 0x80;
  1205. if(mv[0] != mv[dm*mv_stride] || mv[dm] != mv[dm*(mv_stride+1)])
  1206. *(uint64_t*)(pict->data[0] + sx + (sy + 4)*pict->linesize[0])^= 0x8080808080808080ULL;
  1207. }
  1208. }
  1209. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
  1210. // hmm
  1211. }
  1212. }
  1213. s->mbskip_table[mb_index]=0;
  1214. }
  1215. }
  1216. }
  1217. }
  1218. /**
  1219. * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples.
  1220. * @param buf destination buffer
  1221. * @param src source buffer
  1222. * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers
  1223. * @param block_w width of block
  1224. * @param block_h height of block
  1225. * @param src_x x coordinate of the top left sample of the block in the source buffer
  1226. * @param src_y y coordinate of the top left sample of the block in the source buffer
  1227. * @param w width of the source buffer
  1228. * @param h height of the source buffer
  1229. */
  1230. void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
  1231. int src_x, int src_y, int w, int h){
  1232. int x, y;
  1233. int start_y, start_x, end_y, end_x;
  1234. if(src_y>= h){
  1235. src+= (h-1-src_y)*linesize;
  1236. src_y=h-1;
  1237. }else if(src_y<=-block_h){
  1238. src+= (1-block_h-src_y)*linesize;
  1239. src_y=1-block_h;
  1240. }
  1241. if(src_x>= w){
  1242. src+= (w-1-src_x);
  1243. src_x=w-1;
  1244. }else if(src_x<=-block_w){
  1245. src+= (1-block_w-src_x);
  1246. src_x=1-block_w;
  1247. }
  1248. start_y= FFMAX(0, -src_y);
  1249. start_x= FFMAX(0, -src_x);
  1250. end_y= FFMIN(block_h, h-src_y);
  1251. end_x= FFMIN(block_w, w-src_x);
  1252. // copy existing part
  1253. for(y=start_y; y<end_y; y++){
  1254. for(x=start_x; x<end_x; x++){
  1255. buf[x + y*linesize]= src[x + y*linesize];
  1256. }
  1257. }
  1258. //top
  1259. for(y=0; y<start_y; y++){
  1260. for(x=start_x; x<end_x; x++){
  1261. buf[x + y*linesize]= buf[x + start_y*linesize];
  1262. }
  1263. }
  1264. //bottom
  1265. for(y=end_y; y<block_h; y++){
  1266. for(x=start_x; x<end_x; x++){
  1267. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  1268. }
  1269. }
  1270. for(y=0; y<block_h; y++){
  1271. //left
  1272. for(x=0; x<start_x; x++){
  1273. buf[x + y*linesize]= buf[start_x + y*linesize];
  1274. }
  1275. //right
  1276. for(x=end_x; x<block_w; x++){
  1277. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  1278. }
  1279. }
  1280. }
  1281. static inline int hpel_motion_lowres(MpegEncContext *s,
  1282. uint8_t *dest, uint8_t *src,
  1283. int field_based, int field_select,
  1284. int src_x, int src_y,
  1285. int width, int height, int stride,
  1286. int h_edge_pos, int v_edge_pos,
  1287. int w, int h, h264_chroma_mc_func *pix_op,
  1288. int motion_x, int motion_y)
  1289. {
  1290. const int lowres= s->avctx->lowres;
  1291. const int s_mask= (2<<lowres)-1;
  1292. int emu=0;
  1293. int sx, sy;
  1294. if(s->quarter_sample){
  1295. motion_x/=2;
  1296. motion_y/=2;
  1297. }
  1298. sx= motion_x & s_mask;
  1299. sy= motion_y & s_mask;
  1300. src_x += motion_x >> (lowres+1);
  1301. src_y += motion_y >> (lowres+1);
  1302. src += src_y * stride + src_x;
  1303. if( (unsigned)src_x > h_edge_pos - (!!sx) - w
  1304. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1305. ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  1306. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1307. src= s->edge_emu_buffer;
  1308. emu=1;
  1309. }
  1310. sx <<= 2 - lowres;
  1311. sy <<= 2 - lowres;
  1312. if(field_select)
  1313. src += s->linesize;
  1314. pix_op[lowres](dest, src, stride, h, sx, sy);
  1315. return emu;
  1316. }
  1317. /* apply one mpeg motion vector to the three components */
  1318. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1319. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1320. int field_based, int bottom_field, int field_select,
  1321. uint8_t **ref_picture, h264_chroma_mc_func *pix_op,
  1322. int motion_x, int motion_y, int h)
  1323. {
  1324. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1325. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy, uvsx, uvsy;
  1326. const int lowres= s->avctx->lowres;
  1327. const int block_s= 8>>lowres;
  1328. const int s_mask= (2<<lowres)-1;
  1329. const int h_edge_pos = s->h_edge_pos >> lowres;
  1330. const int v_edge_pos = s->v_edge_pos >> lowres;
  1331. linesize = s->current_picture.linesize[0] << field_based;
  1332. uvlinesize = s->current_picture.linesize[1] << field_based;
  1333. if(s->quarter_sample){ //FIXME obviously not perfect but qpel wont work in lowres anyway
  1334. motion_x/=2;
  1335. motion_y/=2;
  1336. }
  1337. if(field_based){
  1338. motion_y += (bottom_field - field_select)*((1<<lowres)-1);
  1339. }
  1340. sx= motion_x & s_mask;
  1341. sy= motion_y & s_mask;
  1342. src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1));
  1343. src_y =(s->mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1));
  1344. if (s->out_format == FMT_H263) {
  1345. uvsx = ((motion_x>>1) & s_mask) | (sx&1);
  1346. uvsy = ((motion_y>>1) & s_mask) | (sy&1);
  1347. uvsrc_x = src_x>>1;
  1348. uvsrc_y = src_y>>1;
  1349. }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  1350. mx = motion_x / 4;
  1351. my = motion_y / 4;
  1352. uvsx = (2*mx) & s_mask;
  1353. uvsy = (2*my) & s_mask;
  1354. uvsrc_x = s->mb_x*block_s + (mx >> lowres);
  1355. uvsrc_y = s->mb_y*block_s + (my >> lowres);
  1356. } else {
  1357. mx = motion_x / 2;
  1358. my = motion_y / 2;
  1359. uvsx = mx & s_mask;
  1360. uvsy = my & s_mask;
  1361. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1362. uvsrc_y =(s->mb_y*block_s>>field_based) + (my >> (lowres+1));
  1363. }
  1364. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1365. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1366. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1367. if( (unsigned)src_x > h_edge_pos - (!!sx) - 2*block_s
  1368. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1369. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  1370. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1371. ptr_y = s->edge_emu_buffer;
  1372. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1373. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  1374. ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
  1375. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1376. ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
  1377. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1378. ptr_cb= uvbuf;
  1379. ptr_cr= uvbuf+16;
  1380. }
  1381. }
  1382. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
  1383. dest_y += s->linesize;
  1384. dest_cb+= s->uvlinesize;
  1385. dest_cr+= s->uvlinesize;
  1386. }
  1387. if(field_select){
  1388. ptr_y += s->linesize;
  1389. ptr_cb+= s->uvlinesize;
  1390. ptr_cr+= s->uvlinesize;
  1391. }
  1392. sx <<= 2 - lowres;
  1393. sy <<= 2 - lowres;
  1394. pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy);
  1395. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1396. uvsx <<= 2 - lowres;
  1397. uvsy <<= 2 - lowres;
  1398. pix_op[lowres](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1399. pix_op[lowres](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1400. }
  1401. //FIXME h261 lowres loop filter
  1402. }
  1403. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1404. uint8_t *dest_cb, uint8_t *dest_cr,
  1405. uint8_t **ref_picture,
  1406. h264_chroma_mc_func *pix_op,
  1407. int mx, int my){
  1408. const int lowres= s->avctx->lowres;
  1409. const int block_s= 8>>lowres;
  1410. const int s_mask= (2<<lowres)-1;
  1411. const int h_edge_pos = s->h_edge_pos >> (lowres+1);
  1412. const int v_edge_pos = s->v_edge_pos >> (lowres+1);
  1413. int emu=0, src_x, src_y, offset, sx, sy;
  1414. uint8_t *ptr;
  1415. if(s->quarter_sample){
  1416. mx/=2;
  1417. my/=2;
  1418. }
  1419. /* In case of 8X8, we construct a single chroma motion vector
  1420. with a special rounding */
  1421. mx= ff_h263_round_chroma(mx);
  1422. my= ff_h263_round_chroma(my);
  1423. sx= mx & s_mask;
  1424. sy= my & s_mask;
  1425. src_x = s->mb_x*block_s + (mx >> (lowres+1));
  1426. src_y = s->mb_y*block_s + (my >> (lowres+1));
  1427. offset = src_y * s->uvlinesize + src_x;
  1428. ptr = ref_picture[1] + offset;
  1429. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1430. if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s
  1431. || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){
  1432. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1433. ptr= s->edge_emu_buffer;
  1434. emu=1;
  1435. }
  1436. }
  1437. sx <<= 2 - lowres;
  1438. sy <<= 2 - lowres;
  1439. pix_op[lowres](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1440. ptr = ref_picture[2] + offset;
  1441. if(emu){
  1442. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1443. ptr= s->edge_emu_buffer;
  1444. }
  1445. pix_op[lowres](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1446. }
  1447. /**
  1448. * motion compensation of a single macroblock
  1449. * @param s context
  1450. * @param dest_y luma destination pointer
  1451. * @param dest_cb chroma cb/u destination pointer
  1452. * @param dest_cr chroma cr/v destination pointer
  1453. * @param dir direction (0->forward, 1->backward)
  1454. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1455. * @param pic_op halfpel motion compensation function (average or put normally)
  1456. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1457. */
  1458. static inline void MPV_motion_lowres(MpegEncContext *s,
  1459. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1460. int dir, uint8_t **ref_picture,
  1461. h264_chroma_mc_func *pix_op)
  1462. {
  1463. int mx, my;
  1464. int mb_x, mb_y, i;
  1465. const int lowres= s->avctx->lowres;
  1466. const int block_s= 8>>lowres;
  1467. mb_x = s->mb_x;
  1468. mb_y = s->mb_y;
  1469. switch(s->mv_type) {
  1470. case MV_TYPE_16X16:
  1471. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1472. 0, 0, 0,
  1473. ref_picture, pix_op,
  1474. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
  1475. break;
  1476. case MV_TYPE_8X8:
  1477. mx = 0;
  1478. my = 0;
  1479. for(i=0;i<4;i++) {
  1480. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) * s->linesize)*block_s,
  1481. ref_picture[0], 0, 0,
  1482. (2*mb_x + (i & 1))*block_s, (2*mb_y + (i >>1))*block_s,
  1483. s->width, s->height, s->linesize,
  1484. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1485. block_s, block_s, pix_op,
  1486. s->mv[dir][i][0], s->mv[dir][i][1]);
  1487. mx += s->mv[dir][i][0];
  1488. my += s->mv[dir][i][1];
  1489. }
  1490. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  1491. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my);
  1492. break;
  1493. case MV_TYPE_FIELD:
  1494. if (s->picture_structure == PICT_FRAME) {
  1495. /* top field */
  1496. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1497. 1, 0, s->field_select[dir][0],
  1498. ref_picture, pix_op,
  1499. s->mv[dir][0][0], s->mv[dir][0][1], block_s);
  1500. /* bottom field */
  1501. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1502. 1, 1, s->field_select[dir][1],
  1503. ref_picture, pix_op,
  1504. s->mv[dir][1][0], s->mv[dir][1][1], block_s);
  1505. } else {
  1506. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){
  1507. ref_picture= s->current_picture_ptr->data;
  1508. }
  1509. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1510. 0, 0, s->field_select[dir][0],
  1511. ref_picture, pix_op,
  1512. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
  1513. }
  1514. break;
  1515. case MV_TYPE_16X8:
  1516. for(i=0; i<2; i++){
  1517. uint8_t ** ref2picture;
  1518. if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){
  1519. ref2picture= ref_picture;
  1520. }else{
  1521. ref2picture= s->current_picture_ptr->data;
  1522. }
  1523. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1524. 0, 0, s->field_select[dir][i],
  1525. ref2picture, pix_op,
  1526. s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s);
  1527. dest_y += 2*block_s*s->linesize;
  1528. dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1529. dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1530. }
  1531. break;
  1532. case MV_TYPE_DMV:
  1533. if(s->picture_structure == PICT_FRAME){
  1534. for(i=0; i<2; i++){
  1535. int j;
  1536. for(j=0; j<2; j++){
  1537. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1538. 1, j, j^i,
  1539. ref_picture, pix_op,
  1540. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], block_s);
  1541. }
  1542. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1543. }
  1544. }else{
  1545. for(i=0; i<2; i++){
  1546. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1547. 0, 0, s->picture_structure != i+1,
  1548. ref_picture, pix_op,
  1549. s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s);
  1550. // after put we make avg of the same block
  1551. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1552. //opposite parity is always in the same frame if this is second field
  1553. if(!s->first_field){
  1554. ref_picture = s->current_picture_ptr->data;
  1555. }
  1556. }
  1557. }
  1558. break;
  1559. default: assert(0);
  1560. }
  1561. }
  1562. /* put block[] to dest[] */
  1563. static inline void put_dct(MpegEncContext *s,
  1564. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1565. {
  1566. s->dct_unquantize_intra(s, block, i, qscale);
  1567. s->dsp.idct_put (dest, line_size, block);
  1568. }
  1569. /* add block[] to dest[] */
  1570. static inline void add_dct(MpegEncContext *s,
  1571. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1572. {
  1573. if (s->block_last_index[i] >= 0) {
  1574. s->dsp.idct_add (dest, line_size, block);
  1575. }
  1576. }
  1577. static inline void add_dequant_dct(MpegEncContext *s,
  1578. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1579. {
  1580. if (s->block_last_index[i] >= 0) {
  1581. s->dct_unquantize_inter(s, block, i, qscale);
  1582. s->dsp.idct_add (dest, line_size, block);
  1583. }
  1584. }
  1585. /**
  1586. * cleans dc, ac, coded_block for the current non intra MB
  1587. */
  1588. void ff_clean_intra_table_entries(MpegEncContext *s)
  1589. {
  1590. int wrap = s->b8_stride;
  1591. int xy = s->block_index[0];
  1592. s->dc_val[0][xy ] =
  1593. s->dc_val[0][xy + 1 ] =
  1594. s->dc_val[0][xy + wrap] =
  1595. s->dc_val[0][xy + 1 + wrap] = 1024;
  1596. /* ac pred */
  1597. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1598. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1599. if (s->msmpeg4_version>=3) {
  1600. s->coded_block[xy ] =
  1601. s->coded_block[xy + 1 ] =
  1602. s->coded_block[xy + wrap] =
  1603. s->coded_block[xy + 1 + wrap] = 0;
  1604. }
  1605. /* chroma */
  1606. wrap = s->mb_stride;
  1607. xy = s->mb_x + s->mb_y * wrap;
  1608. s->dc_val[1][xy] =
  1609. s->dc_val[2][xy] = 1024;
  1610. /* ac pred */
  1611. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1612. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1613. s->mbintra_table[xy]= 0;
  1614. }
  1615. /* generic function called after a macroblock has been parsed by the
  1616. decoder or after it has been encoded by the encoder.
  1617. Important variables used:
  1618. s->mb_intra : true if intra macroblock
  1619. s->mv_dir : motion vector direction
  1620. s->mv_type : motion vector type
  1621. s->mv : motion vector
  1622. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1623. */
  1624. static av_always_inline void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64], int lowres_flag)
  1625. {
  1626. int mb_x, mb_y;
  1627. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1628. #ifdef HAVE_XVMC
  1629. if(s->avctx->xvmc_acceleration){
  1630. XVMC_decode_mb(s);//xvmc uses pblocks
  1631. return;
  1632. }
  1633. #endif
  1634. mb_x = s->mb_x;
  1635. mb_y = s->mb_y;
  1636. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1637. /* save DCT coefficients */
  1638. int i,j;
  1639. DCTELEM *dct = &s->current_picture.dct_coeff[mb_xy*64*6];
  1640. for(i=0; i<6; i++)
  1641. for(j=0; j<64; j++)
  1642. *dct++ = block[i][s->dsp.idct_permutation[j]];
  1643. }
  1644. s->current_picture.qscale_table[mb_xy]= s->qscale;
  1645. /* update DC predictors for P macroblocks */
  1646. if (!s->mb_intra) {
  1647. if (s->h263_pred || s->h263_aic) {
  1648. if(s->mbintra_table[mb_xy])
  1649. ff_clean_intra_table_entries(s);
  1650. } else {
  1651. s->last_dc[0] =
  1652. s->last_dc[1] =
  1653. s->last_dc[2] = 128 << s->intra_dc_precision;
  1654. }
  1655. }
  1656. else if (s->h263_pred || s->h263_aic)
  1657. s->mbintra_table[mb_xy]=1;
  1658. 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
  1659. uint8_t *dest_y, *dest_cb, *dest_cr;
  1660. int dct_linesize, dct_offset;
  1661. op_pixels_func (*op_pix)[4];
  1662. qpel_mc_func (*op_qpix)[16];
  1663. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
  1664. const int uvlinesize= s->current_picture.linesize[1];
  1665. const int readable= s->pict_type != B_TYPE || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  1666. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  1667. /* avoid copy if macroblock skipped in last frame too */
  1668. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1669. if(!s->encoding){
  1670. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1671. const int age= s->current_picture.age;
  1672. assert(age);
  1673. if (s->mb_skipped) {
  1674. s->mb_skipped= 0;
  1675. assert(s->pict_type!=I_TYPE);
  1676. (*mbskip_ptr) ++; /* indicate that this time we skipped it */
  1677. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1678. /* if previous was skipped too, then nothing to do ! */
  1679. if (*mbskip_ptr >= age && s->current_picture.reference){
  1680. return;
  1681. }
  1682. } else if(!s->current_picture.reference){
  1683. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  1684. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1685. } else{
  1686. *mbskip_ptr = 0; /* not skipped */
  1687. }
  1688. }
  1689. dct_linesize = linesize << s->interlaced_dct;
  1690. dct_offset =(s->interlaced_dct)? linesize : linesize*block_size;
  1691. if(readable){
  1692. dest_y= s->dest[0];
  1693. dest_cb= s->dest[1];
  1694. dest_cr= s->dest[2];
  1695. }else{
  1696. dest_y = s->b_scratchpad;
  1697. dest_cb= s->b_scratchpad+16*linesize;
  1698. dest_cr= s->b_scratchpad+32*linesize;
  1699. }
  1700. if (!s->mb_intra) {
  1701. /* motion handling */
  1702. /* decoding or more than one mb_type (MC was already done otherwise) */
  1703. if(!s->encoding){
  1704. if(lowres_flag){
  1705. h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
  1706. if (s->mv_dir & MV_DIR_FORWARD) {
  1707. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix);
  1708. op_pix = s->dsp.avg_h264_chroma_pixels_tab;
  1709. }
  1710. if (s->mv_dir & MV_DIR_BACKWARD) {
  1711. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix);
  1712. }
  1713. }else{
  1714. op_qpix= s->me.qpel_put;
  1715. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1716. op_pix = s->dsp.put_pixels_tab;
  1717. }else{
  1718. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1719. }
  1720. if (s->mv_dir & MV_DIR_FORWARD) {
  1721. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  1722. op_pix = s->dsp.avg_pixels_tab;
  1723. op_qpix= s->me.qpel_avg;
  1724. }
  1725. if (s->mv_dir & MV_DIR_BACKWARD) {
  1726. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  1727. }
  1728. }
  1729. }
  1730. /* skip dequant / idct if we are really late ;) */
  1731. if(s->hurry_up>1) goto skip_idct;
  1732. if(s->avctx->skip_idct){
  1733. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == B_TYPE)
  1734. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != I_TYPE)
  1735. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1736. goto skip_idct;
  1737. }
  1738. /* add dct residue */
  1739. if(s->encoding || !( s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  1740. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1741. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1742. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1743. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1744. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1745. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1746. if (s->chroma_y_shift){
  1747. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1748. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1749. }else{
  1750. dct_linesize >>= 1;
  1751. dct_offset >>=1;
  1752. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1753. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1754. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1755. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1756. }
  1757. }
  1758. } else if(s->codec_id != CODEC_ID_WMV2){
  1759. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1760. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1761. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1762. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1763. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1764. if(s->chroma_y_shift){//Chroma420
  1765. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1766. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1767. }else{
  1768. //chroma422
  1769. dct_linesize = uvlinesize << s->interlaced_dct;
  1770. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  1771. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1772. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1773. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1774. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1775. if(!s->chroma_x_shift){//Chroma444
  1776. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  1777. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  1778. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  1779. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  1780. }
  1781. }
  1782. }//fi gray
  1783. }
  1784. else if (ENABLE_WMV2) {
  1785. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1786. }
  1787. } else {
  1788. /* dct only in intra block */
  1789. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  1790. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1791. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1792. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1793. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1794. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1795. if(s->chroma_y_shift){
  1796. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1797. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1798. }else{
  1799. dct_offset >>=1;
  1800. dct_linesize >>=1;
  1801. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1802. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1803. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1804. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1805. }
  1806. }
  1807. }else{
  1808. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1809. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1810. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1811. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1812. if(!ENABLE_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1813. if(s->chroma_y_shift){
  1814. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1815. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1816. }else{
  1817. dct_linesize = uvlinesize << s->interlaced_dct;
  1818. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  1819. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  1820. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  1821. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1822. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1823. if(!s->chroma_x_shift){//Chroma444
  1824. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  1825. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  1826. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  1827. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  1828. }
  1829. }
  1830. }//gray
  1831. }
  1832. }
  1833. skip_idct:
  1834. if(!readable){
  1835. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1836. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1837. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1838. }
  1839. }
  1840. }
  1841. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
  1842. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1);
  1843. else MPV_decode_mb_internal(s, block, 0);
  1844. }
  1845. /**
  1846. *
  1847. * @param h is the normal height, this will be reduced automatically if needed for the last row
  1848. */
  1849. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  1850. if (s->avctx->draw_horiz_band) {
  1851. AVFrame *src;
  1852. int offset[4];
  1853. if(s->picture_structure != PICT_FRAME){
  1854. h <<= 1;
  1855. y <<= 1;
  1856. if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  1857. }
  1858. h= FFMIN(h, s->avctx->height - y);
  1859. if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  1860. src= (AVFrame*)s->current_picture_ptr;
  1861. else if(s->last_picture_ptr)
  1862. src= (AVFrame*)s->last_picture_ptr;
  1863. else
  1864. return;
  1865. if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  1866. offset[0]=
  1867. offset[1]=
  1868. offset[2]=
  1869. offset[3]= 0;
  1870. }else{
  1871. offset[0]= y * s->linesize;;
  1872. offset[1]=
  1873. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  1874. offset[3]= 0;
  1875. }
  1876. emms_c();
  1877. s->avctx->draw_horiz_band(s->avctx, src, offset,
  1878. y, s->picture_structure, h);
  1879. }
  1880. }
  1881. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  1882. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
  1883. const int uvlinesize= s->current_picture.linesize[1];
  1884. const int mb_size= 4 - s->avctx->lowres;
  1885. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  1886. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  1887. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  1888. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  1889. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  1890. 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;
  1891. //block_index is not used by mpeg2, so it is not affected by chroma_format
  1892. s->dest[0] = s->current_picture.data[0] + ((s->mb_x - 1) << mb_size);
  1893. s->dest[1] = s->current_picture.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  1894. s->dest[2] = s->current_picture.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  1895. if(!(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  1896. {
  1897. s->dest[0] += s->mb_y * linesize << mb_size;
  1898. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1899. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1900. }
  1901. }
  1902. void ff_mpeg_flush(AVCodecContext *avctx){
  1903. int i;
  1904. MpegEncContext *s = avctx->priv_data;
  1905. if(s==NULL || s->picture==NULL)
  1906. return;
  1907. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1908. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  1909. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  1910. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  1911. }
  1912. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  1913. s->mb_x= s->mb_y= 0;
  1914. s->parse_context.state= -1;
  1915. s->parse_context.frame_start_found= 0;
  1916. s->parse_context.overread= 0;
  1917. s->parse_context.overread_index= 0;
  1918. s->parse_context.index= 0;
  1919. s->parse_context.last_index= 0;
  1920. s->bitstream_buffer_size=0;
  1921. s->pp_time=0;
  1922. }
  1923. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  1924. DCTELEM *block, int n, int qscale)
  1925. {
  1926. int i, level, nCoeffs;
  1927. const uint16_t *quant_matrix;
  1928. nCoeffs= s->block_last_index[n];
  1929. if (n < 4)
  1930. block[0] = block[0] * s->y_dc_scale;
  1931. else
  1932. block[0] = block[0] * s->c_dc_scale;
  1933. /* XXX: only mpeg1 */
  1934. quant_matrix = s->intra_matrix;
  1935. for(i=1;i<=nCoeffs;i++) {
  1936. int j= s->intra_scantable.permutated[i];
  1937. level = block[j];
  1938. if (level) {
  1939. if (level < 0) {
  1940. level = -level;
  1941. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1942. level = (level - 1) | 1;
  1943. level = -level;
  1944. } else {
  1945. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1946. level = (level - 1) | 1;
  1947. }
  1948. block[j] = level;
  1949. }
  1950. }
  1951. }
  1952. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  1953. DCTELEM *block, int n, int qscale)
  1954. {
  1955. int i, level, nCoeffs;
  1956. const uint16_t *quant_matrix;
  1957. nCoeffs= s->block_last_index[n];
  1958. quant_matrix = s->inter_matrix;
  1959. for(i=0; i<=nCoeffs; i++) {
  1960. int j= s->intra_scantable.permutated[i];
  1961. level = block[j];
  1962. if (level) {
  1963. if (level < 0) {
  1964. level = -level;
  1965. level = (((level << 1) + 1) * qscale *
  1966. ((int) (quant_matrix[j]))) >> 4;
  1967. level = (level - 1) | 1;
  1968. level = -level;
  1969. } else {
  1970. level = (((level << 1) + 1) * qscale *
  1971. ((int) (quant_matrix[j]))) >> 4;
  1972. level = (level - 1) | 1;
  1973. }
  1974. block[j] = level;
  1975. }
  1976. }
  1977. }
  1978. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  1979. DCTELEM *block, int n, int qscale)
  1980. {
  1981. int i, level, nCoeffs;
  1982. const uint16_t *quant_matrix;
  1983. if(s->alternate_scan) nCoeffs= 63;
  1984. else nCoeffs= s->block_last_index[n];
  1985. if (n < 4)
  1986. block[0] = block[0] * s->y_dc_scale;
  1987. else
  1988. block[0] = block[0] * s->c_dc_scale;
  1989. quant_matrix = s->intra_matrix;
  1990. for(i=1;i<=nCoeffs;i++) {
  1991. int j= s->intra_scantable.permutated[i];
  1992. level = block[j];
  1993. if (level) {
  1994. if (level < 0) {
  1995. level = -level;
  1996. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1997. level = -level;
  1998. } else {
  1999. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2000. }
  2001. block[j] = level;
  2002. }
  2003. }
  2004. }
  2005. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2006. DCTELEM *block, int n, int qscale)
  2007. {
  2008. int i, level, nCoeffs;
  2009. const uint16_t *quant_matrix;
  2010. int sum=-1;
  2011. if(s->alternate_scan) nCoeffs= 63;
  2012. else nCoeffs= s->block_last_index[n];
  2013. if (n < 4)
  2014. block[0] = block[0] * s->y_dc_scale;
  2015. else
  2016. block[0] = block[0] * s->c_dc_scale;
  2017. quant_matrix = s->intra_matrix;
  2018. for(i=1;i<=nCoeffs;i++) {
  2019. int j= s->intra_scantable.permutated[i];
  2020. level = block[j];
  2021. if (level) {
  2022. if (level < 0) {
  2023. level = -level;
  2024. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2025. level = -level;
  2026. } else {
  2027. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2028. }
  2029. block[j] = level;
  2030. sum+=level;
  2031. }
  2032. }
  2033. block[63]^=sum&1;
  2034. }
  2035. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2036. DCTELEM *block, int n, int qscale)
  2037. {
  2038. int i, level, nCoeffs;
  2039. const uint16_t *quant_matrix;
  2040. int sum=-1;
  2041. if(s->alternate_scan) nCoeffs= 63;
  2042. else nCoeffs= s->block_last_index[n];
  2043. quant_matrix = s->inter_matrix;
  2044. for(i=0; i<=nCoeffs; i++) {
  2045. int j= s->intra_scantable.permutated[i];
  2046. level = block[j];
  2047. if (level) {
  2048. if (level < 0) {
  2049. level = -level;
  2050. level = (((level << 1) + 1) * qscale *
  2051. ((int) (quant_matrix[j]))) >> 4;
  2052. level = -level;
  2053. } else {
  2054. level = (((level << 1) + 1) * qscale *
  2055. ((int) (quant_matrix[j]))) >> 4;
  2056. }
  2057. block[j] = level;
  2058. sum+=level;
  2059. }
  2060. }
  2061. block[63]^=sum&1;
  2062. }
  2063. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2064. DCTELEM *block, int n, int qscale)
  2065. {
  2066. int i, level, qmul, qadd;
  2067. int nCoeffs;
  2068. assert(s->block_last_index[n]>=0);
  2069. qmul = qscale << 1;
  2070. if (!s->h263_aic) {
  2071. if (n < 4)
  2072. block[0] = block[0] * s->y_dc_scale;
  2073. else
  2074. block[0] = block[0] * s->c_dc_scale;
  2075. qadd = (qscale - 1) | 1;
  2076. }else{
  2077. qadd = 0;
  2078. }
  2079. if(s->ac_pred)
  2080. nCoeffs=63;
  2081. else
  2082. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2083. for(i=1; i<=nCoeffs; i++) {
  2084. level = block[i];
  2085. if (level) {
  2086. if (level < 0) {
  2087. level = level * qmul - qadd;
  2088. } else {
  2089. level = level * qmul + qadd;
  2090. }
  2091. block[i] = level;
  2092. }
  2093. }
  2094. }
  2095. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2096. DCTELEM *block, int n, int qscale)
  2097. {
  2098. int i, level, qmul, qadd;
  2099. int nCoeffs;
  2100. assert(s->block_last_index[n]>=0);
  2101. qadd = (qscale - 1) | 1;
  2102. qmul = qscale << 1;
  2103. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2104. for(i=0; i<=nCoeffs; i++) {
  2105. level = block[i];
  2106. if (level) {
  2107. if (level < 0) {
  2108. level = level * qmul - qadd;
  2109. } else {
  2110. level = level * qmul + qadd;
  2111. }
  2112. block[i] = level;
  2113. }
  2114. }
  2115. }
  2116. /**
  2117. * set qscale and update qscale dependent variables.
  2118. */
  2119. void ff_set_qscale(MpegEncContext * s, int qscale)
  2120. {
  2121. if (qscale < 1)
  2122. qscale = 1;
  2123. else if (qscale > 31)
  2124. qscale = 31;
  2125. s->qscale = qscale;
  2126. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2127. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2128. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2129. }