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.

2430 lines
90KB

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