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.

6614 lines
239KB

  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. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  21. */
  22. /**
  23. * @file mpegvideo.c
  24. * The simplest mpeg encoder (well, it was the simplest!).
  25. */
  26. #include "avcodec.h"
  27. #include "dsputil.h"
  28. #include "mpegvideo.h"
  29. #include "faandct.h"
  30. #include <limits.h>
  31. #ifdef USE_FASTMEMCPY
  32. #include "fastmemcpy.h"
  33. #endif
  34. //#undef NDEBUG
  35. //#include <assert.h>
  36. #ifdef CONFIG_ENCODERS
  37. static void encode_picture(MpegEncContext *s, int picture_number);
  38. #endif //CONFIG_ENCODERS
  39. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  40. DCTELEM *block, int n, int qscale);
  41. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  42. DCTELEM *block, int n, int qscale);
  43. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  44. DCTELEM *block, int n, int qscale);
  45. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  46. DCTELEM *block, int n, int qscale);
  47. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  48. DCTELEM *block, int n, int qscale);
  49. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  50. DCTELEM *block, int n, int qscale);
  51. static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
  52. #ifdef CONFIG_ENCODERS
  53. static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  54. static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  55. static int dct_quantize_refine(MpegEncContext *s, DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale);
  56. static int sse_mb(MpegEncContext *s);
  57. static void denoise_dct_c(MpegEncContext *s, DCTELEM *block);
  58. #endif //CONFIG_ENCODERS
  59. #ifdef HAVE_XVMC
  60. extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
  61. extern void XVMC_field_end(MpegEncContext *s);
  62. extern void XVMC_decode_mb(MpegEncContext *s);
  63. #endif
  64. void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
  65. /* enable all paranoid tests for rounding, overflows, etc... */
  66. //#define PARANOID
  67. //#define DEBUG
  68. /* for jpeg fast DCT */
  69. #define CONST_BITS 14
  70. static const uint16_t aanscales[64] = {
  71. /* precomputed values scaled up by 14 bits */
  72. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  73. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  74. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  75. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  76. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  77. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  78. 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  79. 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
  80. };
  81. static const uint8_t h263_chroma_roundtab[16] = {
  82. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  83. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
  84. };
  85. static const uint8_t ff_default_chroma_qscale_table[32]={
  86. // 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
  87. 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
  88. };
  89. #ifdef CONFIG_ENCODERS
  90. static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
  91. static uint8_t default_fcode_tab[MAX_MV*2+1];
  92. enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
  93. static void convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
  94. const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra)
  95. {
  96. int qscale;
  97. int shift=0;
  98. for(qscale=qmin; qscale<=qmax; qscale++){
  99. int i;
  100. if (dsp->fdct == ff_jpeg_fdct_islow
  101. #ifdef FAAN_POSTSCALE
  102. || dsp->fdct == ff_faandct
  103. #endif
  104. ) {
  105. for(i=0;i<64;i++) {
  106. const int j= dsp->idct_permutation[i];
  107. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  108. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  109. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  110. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  111. qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) /
  112. (qscale * quant_matrix[j]));
  113. }
  114. } else if (dsp->fdct == fdct_ifast
  115. #ifndef FAAN_POSTSCALE
  116. || dsp->fdct == ff_faandct
  117. #endif
  118. ) {
  119. for(i=0;i<64;i++) {
  120. const int j= dsp->idct_permutation[i];
  121. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  122. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  123. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  124. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  125. qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) /
  126. (aanscales[i] * qscale * quant_matrix[j]));
  127. }
  128. } else {
  129. for(i=0;i<64;i++) {
  130. const int j= dsp->idct_permutation[i];
  131. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  132. So 16 <= qscale * quant_matrix[i] <= 7905
  133. so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
  134. so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
  135. */
  136. qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
  137. // qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  138. qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
  139. if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
  140. qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
  141. }
  142. }
  143. for(i=intra; i<64; i++){
  144. int64_t max= 8191;
  145. if (dsp->fdct == fdct_ifast
  146. #ifndef FAAN_POSTSCALE
  147. || dsp->fdct == ff_faandct
  148. #endif
  149. ) {
  150. max= (8191LL*aanscales[i]) >> 14;
  151. }
  152. while(((max * qmat[qscale][i]) >> shift) > INT_MAX){
  153. shift++;
  154. }
  155. }
  156. }
  157. if(shift){
  158. av_log(NULL, AV_LOG_INFO, "Warning, QMAT_SHIFT is larger then %d, overflows possible\n", QMAT_SHIFT - shift);
  159. }
  160. }
  161. static inline void update_qscale(MpegEncContext *s){
  162. s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  163. s->qscale= clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
  164. s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  165. }
  166. #endif //CONFIG_ENCODERS
  167. void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
  168. int i;
  169. int end;
  170. st->scantable= src_scantable;
  171. for(i=0; i<64; i++){
  172. int j;
  173. j = src_scantable[i];
  174. st->permutated[i] = permutation[j];
  175. #ifdef ARCH_POWERPC
  176. st->inverse[j] = i;
  177. #endif
  178. }
  179. end=-1;
  180. for(i=0; i<64; i++){
  181. int j;
  182. j = st->permutated[i];
  183. if(j>end) end=j;
  184. st->raster_end[i]= end;
  185. }
  186. }
  187. #ifdef CONFIG_ENCODERS
  188. void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix){
  189. int i;
  190. if(matrix){
  191. put_bits(pb, 1, 1);
  192. for(i=0;i<64;i++) {
  193. put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
  194. }
  195. }else
  196. put_bits(pb, 1, 0);
  197. }
  198. #endif //CONFIG_ENCODERS
  199. /* init common dct for both encoder and decoder */
  200. int DCT_common_init(MpegEncContext *s)
  201. {
  202. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  203. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  204. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  205. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  206. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  207. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  208. #ifdef CONFIG_ENCODERS
  209. s->dct_quantize= dct_quantize_c;
  210. s->denoise_dct= denoise_dct_c;
  211. #endif
  212. #ifdef HAVE_MMX
  213. MPV_common_init_mmx(s);
  214. #endif
  215. #ifdef ARCH_ALPHA
  216. MPV_common_init_axp(s);
  217. #endif
  218. #ifdef HAVE_MLIB
  219. MPV_common_init_mlib(s);
  220. #endif
  221. #ifdef HAVE_MMI
  222. MPV_common_init_mmi(s);
  223. #endif
  224. #ifdef ARCH_ARMV4L
  225. MPV_common_init_armv4l(s);
  226. #endif
  227. #ifdef ARCH_POWERPC
  228. MPV_common_init_ppc(s);
  229. #endif
  230. #ifdef CONFIG_ENCODERS
  231. s->fast_dct_quantize= s->dct_quantize;
  232. if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
  233. s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
  234. }
  235. #endif //CONFIG_ENCODERS
  236. /* load & permutate scantables
  237. note: only wmv uses differnt ones
  238. */
  239. if(s->alternate_scan){
  240. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  241. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  242. }else{
  243. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  244. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  245. }
  246. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  247. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  248. return 0;
  249. }
  250. static void copy_picture(Picture *dst, Picture *src){
  251. *dst = *src;
  252. dst->type= FF_BUFFER_TYPE_COPY;
  253. }
  254. static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst, AVFrame *src){
  255. int i;
  256. dst->pict_type = src->pict_type;
  257. dst->quality = src->quality;
  258. dst->coded_picture_number = src->coded_picture_number;
  259. dst->display_picture_number = src->display_picture_number;
  260. // dst->reference = src->reference;
  261. dst->pts = src->pts;
  262. dst->interlaced_frame = src->interlaced_frame;
  263. dst->top_field_first = src->top_field_first;
  264. if(s->avctx->me_threshold){
  265. if(!src->motion_val[0])
  266. av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_val not set!\n");
  267. if(!src->mb_type)
  268. av_log(s->avctx, AV_LOG_ERROR, "AVFrame.mb_type not set!\n");
  269. if(!src->ref_index[0])
  270. av_log(s->avctx, AV_LOG_ERROR, "AVFrame.ref_index not set!\n");
  271. if(src->motion_subsample_log2 != dst->motion_subsample_log2)
  272. av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_subsample_log2 doesnt match! (%d!=%d)\n",
  273. src->motion_subsample_log2, dst->motion_subsample_log2);
  274. memcpy(dst->mb_type, src->mb_type, s->mb_stride * s->mb_height * sizeof(dst->mb_type[0]));
  275. for(i=0; i<2; i++){
  276. int stride= ((16*s->mb_width )>>src->motion_subsample_log2) + 1;
  277. int height= ((16*s->mb_height)>>src->motion_subsample_log2);
  278. if(src->motion_val[i] && src->motion_val[i] != dst->motion_val[i]){
  279. memcpy(dst->motion_val[i], src->motion_val[i], 2*stride*height*sizeof(int16_t));
  280. }
  281. if(src->ref_index[i] && src->ref_index[i] != dst->ref_index[i]){
  282. memcpy(dst->ref_index[i], src->ref_index[i], s->b8_stride*2*s->mb_height*sizeof(int8_t));
  283. }
  284. }
  285. }
  286. }
  287. /**
  288. * allocates a Picture
  289. * The pixels are allocated/set by calling get_buffer() if shared=0
  290. */
  291. static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
  292. const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
  293. const int mb_array_size= s->mb_stride*s->mb_height;
  294. const int b8_array_size= s->b8_stride*s->mb_height*2;
  295. const int b4_array_size= s->b4_stride*s->mb_height*4;
  296. int i;
  297. if(shared){
  298. assert(pic->data[0]);
  299. assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
  300. pic->type= FF_BUFFER_TYPE_SHARED;
  301. }else{
  302. int r;
  303. assert(!pic->data[0]);
  304. r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
  305. if(r<0 || !pic->age || !pic->type || !pic->data[0]){
  306. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
  307. return -1;
  308. }
  309. if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
  310. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
  311. return -1;
  312. }
  313. if(pic->linesize[1] != pic->linesize[2]){
  314. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride missmatch)\n");
  315. return -1;
  316. }
  317. s->linesize = pic->linesize[0];
  318. s->uvlinesize= pic->linesize[1];
  319. }
  320. if(pic->qscale_table==NULL){
  321. if (s->encoding) {
  322. CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
  323. CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
  324. CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
  325. }
  326. CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
  327. CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
  328. CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num * sizeof(uint32_t))
  329. pic->mb_type= pic->mb_type_base + s->mb_stride+1;
  330. if(s->out_format == FMT_H264){
  331. for(i=0; i<2; i++){
  332. CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+2) * sizeof(int16_t))
  333. pic->motion_val[i]= pic->motion_val_base[i]+2;
  334. CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
  335. }
  336. pic->motion_subsample_log2= 2;
  337. }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
  338. for(i=0; i<2; i++){
  339. CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+2) * sizeof(int16_t))
  340. pic->motion_val[i]= pic->motion_val_base[i]+2;
  341. CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
  342. }
  343. pic->motion_subsample_log2= 3;
  344. }
  345. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  346. CHECKED_ALLOCZ(pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6)
  347. }
  348. pic->qstride= s->mb_stride;
  349. CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
  350. }
  351. //it might be nicer if the application would keep track of these but it would require a API change
  352. memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
  353. s->prev_pict_types[0]= s->pict_type;
  354. if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
  355. pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
  356. return 0;
  357. fail: //for the CHECKED_ALLOCZ macro
  358. return -1;
  359. }
  360. /**
  361. * deallocates a picture
  362. */
  363. static void free_picture(MpegEncContext *s, Picture *pic){
  364. int i;
  365. if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
  366. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  367. }
  368. av_freep(&pic->mb_var);
  369. av_freep(&pic->mc_mb_var);
  370. av_freep(&pic->mb_mean);
  371. av_freep(&pic->mbskip_table);
  372. av_freep(&pic->qscale_table);
  373. av_freep(&pic->mb_type_base);
  374. av_freep(&pic->dct_coeff);
  375. av_freep(&pic->pan_scan);
  376. pic->mb_type= NULL;
  377. for(i=0; i<2; i++){
  378. av_freep(&pic->motion_val_base[i]);
  379. av_freep(&pic->ref_index[i]);
  380. }
  381. if(pic->type == FF_BUFFER_TYPE_SHARED){
  382. for(i=0; i<4; i++){
  383. pic->base[i]=
  384. pic->data[i]= NULL;
  385. }
  386. pic->type= 0;
  387. }
  388. }
  389. static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
  390. int i;
  391. // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
  392. CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
  393. s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
  394. //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
  395. CHECKED_ALLOCZ(s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t))
  396. s->rd_scratchpad= s->me.scratchpad;
  397. s->b_scratchpad= s->me.scratchpad;
  398. s->obmc_scratchpad= s->me.scratchpad + 16;
  399. if (s->encoding) {
  400. CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
  401. CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
  402. if(s->avctx->noise_reduction){
  403. CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
  404. }
  405. }
  406. CHECKED_ALLOCZ(s->blocks, 64*12*2 * sizeof(DCTELEM))
  407. s->block= s->blocks[0];
  408. for(i=0;i<12;i++){
  409. s->pblocks[i] = (short *)(&s->block[i]);
  410. }
  411. return 0;
  412. fail:
  413. return -1; //free() through MPV_common_end()
  414. }
  415. static void free_duplicate_context(MpegEncContext *s){
  416. if(s==NULL) return;
  417. av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
  418. av_freep(&s->me.scratchpad);
  419. s->rd_scratchpad=
  420. s->b_scratchpad=
  421. s->obmc_scratchpad= NULL;
  422. av_freep(&s->dct_error_sum);
  423. av_freep(&s->me.map);
  424. av_freep(&s->me.score_map);
  425. av_freep(&s->blocks);
  426. s->block= NULL;
  427. }
  428. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src){
  429. #define COPY(a) bak->a= src->a
  430. COPY(allocated_edge_emu_buffer);
  431. COPY(edge_emu_buffer);
  432. COPY(me.scratchpad);
  433. COPY(rd_scratchpad);
  434. COPY(b_scratchpad);
  435. COPY(obmc_scratchpad);
  436. COPY(me.map);
  437. COPY(me.score_map);
  438. COPY(blocks);
  439. COPY(block);
  440. COPY(start_mb_y);
  441. COPY(end_mb_y);
  442. COPY(me.map_generation);
  443. COPY(pb);
  444. COPY(dct_error_sum);
  445. COPY(dct_count[0]);
  446. COPY(dct_count[1]);
  447. #undef COPY
  448. }
  449. void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
  450. MpegEncContext bak;
  451. int i;
  452. //FIXME copy only needed parts
  453. //START_TIMER
  454. backup_duplicate_context(&bak, dst);
  455. memcpy(dst, src, sizeof(MpegEncContext));
  456. backup_duplicate_context(dst, &bak);
  457. for(i=0;i<12;i++){
  458. dst->pblocks[i] = (short *)(&dst->block[i]);
  459. }
  460. //STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  461. }
  462. static void update_duplicate_context_after_me(MpegEncContext *dst, MpegEncContext *src){
  463. #define COPY(a) dst->a= src->a
  464. COPY(pict_type);
  465. COPY(current_picture);
  466. COPY(f_code);
  467. COPY(b_code);
  468. COPY(qscale);
  469. COPY(lambda);
  470. COPY(lambda2);
  471. COPY(picture_in_gop_number);
  472. COPY(gop_picture_number);
  473. COPY(frame_pred_frame_dct); //FIXME dont set in encode_header
  474. COPY(progressive_frame); //FIXME dont set in encode_header
  475. COPY(partitioned_frame); //FIXME dont set in encode_header
  476. #undef COPY
  477. }
  478. /**
  479. * sets the given MpegEncContext to common defaults (same for encoding and decoding).
  480. * the changed fields will not depend upon the prior state of the MpegEncContext.
  481. */
  482. static void MPV_common_defaults(MpegEncContext *s){
  483. s->y_dc_scale_table=
  484. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  485. s->chroma_qscale_table= ff_default_chroma_qscale_table;
  486. s->progressive_frame= 1;
  487. s->progressive_sequence= 1;
  488. s->picture_structure= PICT_FRAME;
  489. s->coded_picture_number = 0;
  490. s->picture_number = 0;
  491. s->input_picture_number = 0;
  492. s->picture_in_gop_number = 0;
  493. s->f_code = 1;
  494. s->b_code = 1;
  495. }
  496. /**
  497. * sets the given MpegEncContext to defaults for decoding.
  498. * the changed fields will not depend upon the prior state of the MpegEncContext.
  499. */
  500. void MPV_decode_defaults(MpegEncContext *s){
  501. MPV_common_defaults(s);
  502. }
  503. /**
  504. * sets the given MpegEncContext to defaults for encoding.
  505. * the changed fields will not depend upon the prior state of the MpegEncContext.
  506. */
  507. #ifdef CONFIG_ENCODERS
  508. static void MPV_encode_defaults(MpegEncContext *s){
  509. static int done=0;
  510. MPV_common_defaults(s);
  511. if(!done){
  512. int i;
  513. done=1;
  514. default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
  515. memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
  516. for(i=-16; i<16; i++){
  517. default_fcode_tab[i + MAX_MV]= 1;
  518. }
  519. }
  520. s->me.mv_penalty= default_mv_penalty;
  521. s->fcode_tab= default_fcode_tab;
  522. }
  523. #endif //CONFIG_ENCODERS
  524. /**
  525. * init common structure for both encoder and decoder.
  526. * this assumes that some variables like width/height are already set
  527. */
  528. int MPV_common_init(MpegEncContext *s)
  529. {
  530. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  531. if(s->avctx->thread_count > MAX_THREADS || (16*s->avctx->thread_count > s->height && s->height)){
  532. av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
  533. return -1;
  534. }
  535. if((s->width || s->height) && avcodec_check_dimensions(s->avctx, s->width, s->height))
  536. return -1;
  537. dsputil_init(&s->dsp, s->avctx);
  538. DCT_common_init(s);
  539. s->flags= s->avctx->flags;
  540. s->flags2= s->avctx->flags2;
  541. s->mb_width = (s->width + 15) / 16;
  542. s->mb_height = (s->height + 15) / 16;
  543. s->mb_stride = s->mb_width + 1;
  544. s->b8_stride = s->mb_width*2 + 1;
  545. s->b4_stride = s->mb_width*4 + 1;
  546. mb_array_size= s->mb_height * s->mb_stride;
  547. mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
  548. /* set chroma shifts */
  549. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
  550. &(s->chroma_y_shift) );
  551. /* set default edge pos, will be overriden in decode_header if needed */
  552. s->h_edge_pos= s->mb_width*16;
  553. s->v_edge_pos= s->mb_height*16;
  554. s->mb_num = s->mb_width * s->mb_height;
  555. s->block_wrap[0]=
  556. s->block_wrap[1]=
  557. s->block_wrap[2]=
  558. s->block_wrap[3]= s->b8_stride;
  559. s->block_wrap[4]=
  560. s->block_wrap[5]= s->mb_stride;
  561. y_size = s->b8_stride * (2 * s->mb_height + 1);
  562. c_size = s->mb_stride * (s->mb_height + 1);
  563. yc_size = y_size + 2 * c_size;
  564. /* convert fourcc to upper case */
  565. s->avctx->codec_tag= toupper( s->avctx->codec_tag &0xFF)
  566. + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
  567. + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
  568. + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
  569. s->avctx->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF)
  570. + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
  571. + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
  572. + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
  573. s->avctx->coded_frame= (AVFrame*)&s->current_picture;
  574. CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this
  575. for(y=0; y<s->mb_height; y++){
  576. for(x=0; x<s->mb_width; x++){
  577. s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
  578. }
  579. }
  580. s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
  581. if (s->encoding) {
  582. /* Allocate MV tables */
  583. CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  584. CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  585. CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  586. CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  587. CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  588. CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  589. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  590. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  591. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  592. s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  593. s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  594. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  595. if(s->msmpeg4_version){
  596. CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
  597. }
  598. CHECKED_ALLOCZ(s->avctx->stats_out, 256);
  599. /* Allocate MB type table */
  600. CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint16_t)) //needed for encoding
  601. CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
  602. CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
  603. CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
  604. CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
  605. CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
  606. CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
  607. CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
  608. if(s->avctx->noise_reduction){
  609. CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
  610. }
  611. }
  612. CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
  613. CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
  614. if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
  615. /* interlaced direct mode decoding tables */
  616. for(i=0; i<2; i++){
  617. int j, k;
  618. for(j=0; j<2; j++){
  619. for(k=0; k<2; k++){
  620. CHECKED_ALLOCZ(s->b_field_mv_table_base[i][j][k] , mv_table_size * 2 * sizeof(int16_t))
  621. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
  622. }
  623. CHECKED_ALLOCZ(s->b_field_select_table[i][j] , mb_array_size * 2 * sizeof(uint8_t))
  624. CHECKED_ALLOCZ(s->p_field_mv_table_base[i][j] , mv_table_size * 2 * sizeof(int16_t))
  625. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
  626. }
  627. CHECKED_ALLOCZ(s->p_field_select_table[i] , mb_array_size * 2 * sizeof(uint8_t))
  628. }
  629. }
  630. if (s->out_format == FMT_H263) {
  631. /* ac values */
  632. CHECKED_ALLOCZ(s->ac_val_base, yc_size * sizeof(int16_t) * 16);
  633. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  634. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  635. s->ac_val[2] = s->ac_val[1] + c_size;
  636. /* cbp values */
  637. CHECKED_ALLOCZ(s->coded_block_base, y_size);
  638. s->coded_block= s->coded_block_base + s->b8_stride + 1;
  639. /* cbp, ac_pred, pred_dir */
  640. CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
  641. CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
  642. }
  643. if (s->h263_pred || s->h263_plus || !s->encoding) {
  644. /* dc values */
  645. //MN: we need these for error resilience of intra-frames
  646. CHECKED_ALLOCZ(s->dc_val_base, yc_size * sizeof(int16_t));
  647. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  648. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  649. s->dc_val[2] = s->dc_val[1] + c_size;
  650. for(i=0;i<yc_size;i++)
  651. s->dc_val_base[i] = 1024;
  652. }
  653. /* which mb is a intra block */
  654. CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
  655. memset(s->mbintra_table, 1, mb_array_size);
  656. /* init macroblock skip table */
  657. CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
  658. //Note the +1 is for a quicker mpeg4 slice_end detection
  659. CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
  660. s->parse_context.state= -1;
  661. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  662. s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  663. s->visualization_buffer[1] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
  664. s->visualization_buffer[2] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
  665. }
  666. s->context_initialized = 1;
  667. s->thread_context[0]= s;
  668. for(i=1; i<s->avctx->thread_count; i++){
  669. s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
  670. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  671. }
  672. for(i=0; i<s->avctx->thread_count; i++){
  673. if(init_duplicate_context(s->thread_context[i], s) < 0)
  674. goto fail;
  675. s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count;
  676. s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
  677. }
  678. return 0;
  679. fail:
  680. MPV_common_end(s);
  681. return -1;
  682. }
  683. /* init common structure for both encoder and decoder */
  684. void MPV_common_end(MpegEncContext *s)
  685. {
  686. int i, j, k;
  687. for(i=0; i<s->avctx->thread_count; i++){
  688. free_duplicate_context(s->thread_context[i]);
  689. }
  690. for(i=1; i<s->avctx->thread_count; i++){
  691. av_freep(&s->thread_context[i]);
  692. }
  693. av_freep(&s->parse_context.buffer);
  694. s->parse_context.buffer_size=0;
  695. av_freep(&s->mb_type);
  696. av_freep(&s->p_mv_table_base);
  697. av_freep(&s->b_forw_mv_table_base);
  698. av_freep(&s->b_back_mv_table_base);
  699. av_freep(&s->b_bidir_forw_mv_table_base);
  700. av_freep(&s->b_bidir_back_mv_table_base);
  701. av_freep(&s->b_direct_mv_table_base);
  702. s->p_mv_table= NULL;
  703. s->b_forw_mv_table= NULL;
  704. s->b_back_mv_table= NULL;
  705. s->b_bidir_forw_mv_table= NULL;
  706. s->b_bidir_back_mv_table= NULL;
  707. s->b_direct_mv_table= NULL;
  708. for(i=0; i<2; i++){
  709. for(j=0; j<2; j++){
  710. for(k=0; k<2; k++){
  711. av_freep(&s->b_field_mv_table_base[i][j][k]);
  712. s->b_field_mv_table[i][j][k]=NULL;
  713. }
  714. av_freep(&s->b_field_select_table[i][j]);
  715. av_freep(&s->p_field_mv_table_base[i][j]);
  716. s->p_field_mv_table[i][j]=NULL;
  717. }
  718. av_freep(&s->p_field_select_table[i]);
  719. }
  720. av_freep(&s->dc_val_base);
  721. av_freep(&s->ac_val_base);
  722. av_freep(&s->coded_block_base);
  723. av_freep(&s->mbintra_table);
  724. av_freep(&s->cbp_table);
  725. av_freep(&s->pred_dir_table);
  726. av_freep(&s->mbskip_table);
  727. av_freep(&s->prev_pict_types);
  728. av_freep(&s->bitstream_buffer);
  729. s->allocated_bitstream_buffer_size=0;
  730. av_freep(&s->avctx->stats_out);
  731. av_freep(&s->ac_stats);
  732. av_freep(&s->error_status_table);
  733. av_freep(&s->mb_index2xy);
  734. av_freep(&s->lambda_table);
  735. av_freep(&s->q_intra_matrix);
  736. av_freep(&s->q_inter_matrix);
  737. av_freep(&s->q_intra_matrix16);
  738. av_freep(&s->q_inter_matrix16);
  739. av_freep(&s->input_picture);
  740. av_freep(&s->reordered_input_picture);
  741. av_freep(&s->dct_offset);
  742. if(s->picture){
  743. for(i=0; i<MAX_PICTURE_COUNT; i++){
  744. free_picture(s, &s->picture[i]);
  745. }
  746. }
  747. av_freep(&s->picture);
  748. s->context_initialized = 0;
  749. s->last_picture_ptr=
  750. s->next_picture_ptr=
  751. s->current_picture_ptr= NULL;
  752. s->linesize= s->uvlinesize= 0;
  753. for(i=0; i<3; i++)
  754. av_freep(&s->visualization_buffer[i]);
  755. avcodec_default_free_buffers(s->avctx);
  756. }
  757. #ifdef CONFIG_ENCODERS
  758. /* init video encoder */
  759. int MPV_encode_init(AVCodecContext *avctx)
  760. {
  761. MpegEncContext *s = avctx->priv_data;
  762. int i, dummy;
  763. int chroma_h_shift, chroma_v_shift;
  764. MPV_encode_defaults(s);
  765. if(avctx->pix_fmt != PIX_FMT_YUVJ420P && avctx->pix_fmt != PIX_FMT_YUV420P){
  766. av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
  767. return -1;
  768. }
  769. if(avctx->codec_id == CODEC_ID_MJPEG || avctx->codec_id == CODEC_ID_LJPEG){
  770. if(avctx->strict_std_compliance>=0 && avctx->pix_fmt != PIX_FMT_YUVJ420P){
  771. av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
  772. return -1;
  773. }
  774. }else{
  775. if(avctx->strict_std_compliance>=0 && avctx->pix_fmt != PIX_FMT_YUV420P){
  776. av_log(avctx, AV_LOG_ERROR, "colorspace not supported\n");
  777. return -1;
  778. }
  779. }
  780. s->bit_rate = avctx->bit_rate;
  781. s->width = avctx->width;
  782. s->height = avctx->height;
  783. if(avctx->gop_size > 600){
  784. av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
  785. avctx->gop_size=600;
  786. }
  787. s->gop_size = avctx->gop_size;
  788. s->avctx = avctx;
  789. s->flags= avctx->flags;
  790. s->flags2= avctx->flags2;
  791. s->max_b_frames= avctx->max_b_frames;
  792. s->codec_id= avctx->codec->id;
  793. s->luma_elim_threshold = avctx->luma_elim_threshold;
  794. s->chroma_elim_threshold= avctx->chroma_elim_threshold;
  795. s->strict_std_compliance= avctx->strict_std_compliance;
  796. s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
  797. s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
  798. s->mpeg_quant= avctx->mpeg_quant;
  799. s->rtp_mode= !!avctx->rtp_payload_size;
  800. s->intra_dc_precision= avctx->intra_dc_precision;
  801. s->user_specified_pts = AV_NOPTS_VALUE;
  802. if (s->gop_size <= 1) {
  803. s->intra_only = 1;
  804. s->gop_size = 12;
  805. } else {
  806. s->intra_only = 0;
  807. }
  808. s->me_method = avctx->me_method;
  809. /* Fixed QSCALE */
  810. s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
  811. s->adaptive_quant= ( s->avctx->lumi_masking
  812. || s->avctx->dark_masking
  813. || s->avctx->temporal_cplx_masking
  814. || s->avctx->spatial_cplx_masking
  815. || s->avctx->p_masking
  816. || s->avctx->border_masking
  817. || (s->flags&CODEC_FLAG_QP_RD))
  818. && !s->fixed_qscale;
  819. s->obmc= !!(s->flags & CODEC_FLAG_OBMC);
  820. s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER);
  821. s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN);
  822. if(avctx->rc_max_rate && !avctx->rc_buffer_size){
  823. av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n");
  824. return -1;
  825. }
  826. if(avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate){
  827. av_log(avctx, AV_LOG_INFO, "Warning min_rate > 0 but min_rate != max_rate isnt recommanded!\n");
  828. }
  829. if(avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate){
  830. av_log(avctx, AV_LOG_INFO, "bitrate below min bitrate\n");
  831. return -1;
  832. }
  833. if(avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate){
  834. av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n");
  835. return -1;
  836. }
  837. if( s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate
  838. && (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO)
  839. && 90000LL * (avctx->rc_buffer_size-1) > s->avctx->rc_max_rate*0xFFFFLL){
  840. av_log(avctx, AV_LOG_INFO, "Warning vbv_delay will be set to 0xFFFF (=VBR) as the specified vbv buffer is too large for the given bitrate!\n");
  841. }
  842. if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4
  843. && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P && s->codec_id != CODEC_ID_FLV1){
  844. av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
  845. return -1;
  846. }
  847. if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
  848. av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decission\n");
  849. return -1;
  850. }
  851. if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
  852. av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n");
  853. return -1;
  854. }
  855. if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
  856. av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
  857. return -1;
  858. }
  859. if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
  860. av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
  861. return -1;
  862. }
  863. if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
  864. av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
  865. return -1;
  866. }
  867. if((s->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN))
  868. && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG2VIDEO){
  869. av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
  870. return -1;
  871. }
  872. if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
  873. av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supporetd by codec\n");
  874. return -1;
  875. }
  876. if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){
  877. av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
  878. return -1;
  879. }
  880. if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
  881. av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
  882. return -1;
  883. }
  884. if(s->avctx->scenechange_threshold < 1000000000 && (s->flags & CODEC_FLAG_CLOSED_GOP)){
  885. av_log(avctx, AV_LOG_ERROR, "closed gop with scene change detection arent supported yet\n");
  886. return -1;
  887. }
  888. if(s->avctx->thread_count > 1 && s->codec_id != CODEC_ID_MPEG4
  889. && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO
  890. && (s->codec_id != CODEC_ID_H263P || !(s->flags & CODEC_FLAG_H263P_SLICE_STRUCT))){
  891. av_log(avctx, AV_LOG_ERROR, "multi threaded encoding not supported by codec\n");
  892. return -1;
  893. }
  894. if(s->avctx->thread_count > 1)
  895. s->rtp_mode= 1;
  896. if(!avctx->frame_rate || !avctx->frame_rate_base){
  897. av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
  898. return -1;
  899. }
  900. i= ff_gcd(avctx->frame_rate, avctx->frame_rate_base);
  901. if(i > 1){
  902. av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
  903. avctx->frame_rate /= i;
  904. avctx->frame_rate_base /= i;
  905. // return -1;
  906. }
  907. if(s->codec_id==CODEC_ID_MJPEG){
  908. s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
  909. s->inter_quant_bias= 0;
  910. }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
  911. s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
  912. s->inter_quant_bias= 0;
  913. }else{
  914. s->intra_quant_bias=0;
  915. s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
  916. }
  917. if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
  918. s->intra_quant_bias= avctx->intra_quant_bias;
  919. if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
  920. s->inter_quant_bias= avctx->inter_quant_bias;
  921. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
  922. av_reduce(&s->time_increment_resolution, &dummy, s->avctx->frame_rate, s->avctx->frame_rate_base, (1<<16)-1);
  923. s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
  924. switch(avctx->codec->id) {
  925. case CODEC_ID_MPEG1VIDEO:
  926. s->out_format = FMT_MPEG1;
  927. s->low_delay= 0; //s->max_b_frames ? 0 : 1;
  928. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  929. break;
  930. case CODEC_ID_MPEG2VIDEO:
  931. s->out_format = FMT_MPEG1;
  932. s->low_delay= 0; //s->max_b_frames ? 0 : 1;
  933. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  934. s->rtp_mode= 1;
  935. break;
  936. case CODEC_ID_LJPEG:
  937. case CODEC_ID_MJPEG:
  938. s->out_format = FMT_MJPEG;
  939. s->intra_only = 1; /* force intra only for jpeg */
  940. s->mjpeg_write_tables = 1; /* write all tables */
  941. s->mjpeg_data_only_frames = 0; /* write all the needed headers */
  942. s->mjpeg_vsample[0] = 1<<chroma_v_shift;
  943. s->mjpeg_vsample[1] = 1;
  944. s->mjpeg_vsample[2] = 1;
  945. s->mjpeg_hsample[0] = 1<<chroma_h_shift;
  946. s->mjpeg_hsample[1] = 1;
  947. s->mjpeg_hsample[2] = 1;
  948. if (mjpeg_init(s) < 0)
  949. return -1;
  950. avctx->delay=0;
  951. s->low_delay=1;
  952. break;
  953. #ifdef CONFIG_RISKY
  954. case CODEC_ID_H261:
  955. s->out_format = FMT_H261;
  956. avctx->delay=0;
  957. s->low_delay=1;
  958. break;
  959. case CODEC_ID_H263:
  960. if (h263_get_picture_format(s->width, s->height) == 7) {
  961. av_log(avctx, AV_LOG_INFO, "Input picture size isn't suitable for h263 codec! try h263+\n");
  962. return -1;
  963. }
  964. s->out_format = FMT_H263;
  965. s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
  966. avctx->delay=0;
  967. s->low_delay=1;
  968. break;
  969. case CODEC_ID_H263P:
  970. s->out_format = FMT_H263;
  971. s->h263_plus = 1;
  972. /* Fx */
  973. s->umvplus = (avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
  974. s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
  975. s->modified_quant= s->h263_aic;
  976. s->alt_inter_vlc= (avctx->flags & CODEC_FLAG_H263P_AIV) ? 1:0;
  977. s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
  978. s->loop_filter= (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1:0;
  979. s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
  980. s->h263_slice_structured= (s->flags & CODEC_FLAG_H263P_SLICE_STRUCT) ? 1:0;
  981. /* /Fx */
  982. /* These are just to be sure */
  983. avctx->delay=0;
  984. s->low_delay=1;
  985. break;
  986. case CODEC_ID_FLV1:
  987. s->out_format = FMT_H263;
  988. s->h263_flv = 2; /* format = 1; 11-bit codes */
  989. s->unrestricted_mv = 1;
  990. s->rtp_mode=0; /* don't allow GOB */
  991. avctx->delay=0;
  992. s->low_delay=1;
  993. break;
  994. case CODEC_ID_RV10:
  995. s->out_format = FMT_H263;
  996. avctx->delay=0;
  997. s->low_delay=1;
  998. break;
  999. case CODEC_ID_RV20:
  1000. s->out_format = FMT_H263;
  1001. avctx->delay=0;
  1002. s->low_delay=1;
  1003. s->modified_quant=1;
  1004. s->h263_aic=1;
  1005. s->h263_plus=1;
  1006. s->loop_filter=1;
  1007. s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
  1008. break;
  1009. case CODEC_ID_MPEG4:
  1010. s->out_format = FMT_H263;
  1011. s->h263_pred = 1;
  1012. s->unrestricted_mv = 1;
  1013. s->low_delay= s->max_b_frames ? 0 : 1;
  1014. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  1015. break;
  1016. case CODEC_ID_MSMPEG4V1:
  1017. s->out_format = FMT_H263;
  1018. s->h263_msmpeg4 = 1;
  1019. s->h263_pred = 1;
  1020. s->unrestricted_mv = 1;
  1021. s->msmpeg4_version= 1;
  1022. avctx->delay=0;
  1023. s->low_delay=1;
  1024. break;
  1025. case CODEC_ID_MSMPEG4V2:
  1026. s->out_format = FMT_H263;
  1027. s->h263_msmpeg4 = 1;
  1028. s->h263_pred = 1;
  1029. s->unrestricted_mv = 1;
  1030. s->msmpeg4_version= 2;
  1031. avctx->delay=0;
  1032. s->low_delay=1;
  1033. break;
  1034. case CODEC_ID_MSMPEG4V3:
  1035. s->out_format = FMT_H263;
  1036. s->h263_msmpeg4 = 1;
  1037. s->h263_pred = 1;
  1038. s->unrestricted_mv = 1;
  1039. s->msmpeg4_version= 3;
  1040. s->flipflop_rounding=1;
  1041. avctx->delay=0;
  1042. s->low_delay=1;
  1043. break;
  1044. case CODEC_ID_WMV1:
  1045. s->out_format = FMT_H263;
  1046. s->h263_msmpeg4 = 1;
  1047. s->h263_pred = 1;
  1048. s->unrestricted_mv = 1;
  1049. s->msmpeg4_version= 4;
  1050. s->flipflop_rounding=1;
  1051. avctx->delay=0;
  1052. s->low_delay=1;
  1053. break;
  1054. case CODEC_ID_WMV2:
  1055. s->out_format = FMT_H263;
  1056. s->h263_msmpeg4 = 1;
  1057. s->h263_pred = 1;
  1058. s->unrestricted_mv = 1;
  1059. s->msmpeg4_version= 5;
  1060. s->flipflop_rounding=1;
  1061. avctx->delay=0;
  1062. s->low_delay=1;
  1063. break;
  1064. #endif
  1065. default:
  1066. return -1;
  1067. }
  1068. avctx->has_b_frames= !s->low_delay;
  1069. s->encoding = 1;
  1070. /* init */
  1071. if (MPV_common_init(s) < 0)
  1072. return -1;
  1073. if(s->modified_quant)
  1074. s->chroma_qscale_table= ff_h263_chroma_qscale_table;
  1075. s->progressive_frame=
  1076. s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME));
  1077. s->quant_precision=5;
  1078. ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
  1079. ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
  1080. #ifdef CONFIG_ENCODERS
  1081. #ifdef CONFIG_RISKY
  1082. if (s->out_format == FMT_H261)
  1083. ff_h261_encode_init(s);
  1084. if (s->out_format == FMT_H263)
  1085. h263_encode_init(s);
  1086. if(s->msmpeg4_version)
  1087. ff_msmpeg4_encode_init(s);
  1088. #endif
  1089. if (s->out_format == FMT_MPEG1)
  1090. ff_mpeg1_encode_init(s);
  1091. #endif
  1092. /* init q matrix */
  1093. for(i=0;i<64;i++) {
  1094. int j= s->dsp.idct_permutation[i];
  1095. #ifdef CONFIG_RISKY
  1096. if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
  1097. s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
  1098. s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
  1099. }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  1100. s->intra_matrix[j] =
  1101. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  1102. }else
  1103. #endif
  1104. { /* mpeg1/2 */
  1105. s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
  1106. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  1107. }
  1108. if(s->avctx->intra_matrix)
  1109. s->intra_matrix[j] = s->avctx->intra_matrix[i];
  1110. if(s->avctx->inter_matrix)
  1111. s->inter_matrix[j] = s->avctx->inter_matrix[i];
  1112. }
  1113. /* precompute matrix */
  1114. /* for mjpeg, we do include qscale in the matrix */
  1115. if (s->out_format != FMT_MJPEG) {
  1116. convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
  1117. s->intra_matrix, s->intra_quant_bias, avctx->qmin, 31, 1);
  1118. convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
  1119. s->inter_matrix, s->inter_quant_bias, avctx->qmin, 31, 0);
  1120. }
  1121. if(ff_rate_control_init(s) < 0)
  1122. return -1;
  1123. return 0;
  1124. }
  1125. int MPV_encode_end(AVCodecContext *avctx)
  1126. {
  1127. MpegEncContext *s = avctx->priv_data;
  1128. #ifdef STATS
  1129. print_stats();
  1130. #endif
  1131. ff_rate_control_uninit(s);
  1132. MPV_common_end(s);
  1133. if (s->out_format == FMT_MJPEG)
  1134. mjpeg_close(s);
  1135. av_freep(&avctx->extradata);
  1136. return 0;
  1137. }
  1138. #endif //CONFIG_ENCODERS
  1139. void init_rl(RLTable *rl, int use_static)
  1140. {
  1141. int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
  1142. uint8_t index_run[MAX_RUN+1];
  1143. int last, run, level, start, end, i;
  1144. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  1145. if(use_static && rl->max_level[0])
  1146. return;
  1147. /* compute max_level[], max_run[] and index_run[] */
  1148. for(last=0;last<2;last++) {
  1149. if (last == 0) {
  1150. start = 0;
  1151. end = rl->last;
  1152. } else {
  1153. start = rl->last;
  1154. end = rl->n;
  1155. }
  1156. memset(max_level, 0, MAX_RUN + 1);
  1157. memset(max_run, 0, MAX_LEVEL + 1);
  1158. memset(index_run, rl->n, MAX_RUN + 1);
  1159. for(i=start;i<end;i++) {
  1160. run = rl->table_run[i];
  1161. level = rl->table_level[i];
  1162. if (index_run[run] == rl->n)
  1163. index_run[run] = i;
  1164. if (level > max_level[run])
  1165. max_level[run] = level;
  1166. if (run > max_run[level])
  1167. max_run[level] = run;
  1168. }
  1169. if(use_static)
  1170. rl->max_level[last] = av_mallocz_static(MAX_RUN + 1);
  1171. else
  1172. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1173. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1174. if(use_static)
  1175. rl->max_run[last] = av_mallocz_static(MAX_LEVEL + 1);
  1176. else
  1177. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1178. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1179. if(use_static)
  1180. rl->index_run[last] = av_mallocz_static(MAX_RUN + 1);
  1181. else
  1182. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1183. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1184. }
  1185. }
  1186. /* draw the edges of width 'w' of an image of size width, height */
  1187. //FIXME check that this is ok for mpeg4 interlaced
  1188. static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
  1189. {
  1190. uint8_t *ptr, *last_line;
  1191. int i;
  1192. last_line = buf + (height - 1) * wrap;
  1193. for(i=0;i<w;i++) {
  1194. /* top and bottom */
  1195. memcpy(buf - (i + 1) * wrap, buf, width);
  1196. memcpy(last_line + (i + 1) * wrap, last_line, width);
  1197. }
  1198. /* left and right */
  1199. ptr = buf;
  1200. for(i=0;i<height;i++) {
  1201. memset(ptr - w, ptr[0], w);
  1202. memset(ptr + width, ptr[width-1], w);
  1203. ptr += wrap;
  1204. }
  1205. /* corners */
  1206. for(i=0;i<w;i++) {
  1207. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  1208. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  1209. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  1210. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  1211. }
  1212. }
  1213. int ff_find_unused_picture(MpegEncContext *s, int shared){
  1214. int i;
  1215. if(shared){
  1216. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1217. if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
  1218. }
  1219. }else{
  1220. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1221. if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
  1222. }
  1223. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1224. if(s->picture[i].data[0]==NULL) return i;
  1225. }
  1226. }
  1227. assert(0);
  1228. return -1;
  1229. }
  1230. static void update_noise_reduction(MpegEncContext *s){
  1231. int intra, i;
  1232. for(intra=0; intra<2; intra++){
  1233. if(s->dct_count[intra] > (1<<16)){
  1234. for(i=0; i<64; i++){
  1235. s->dct_error_sum[intra][i] >>=1;
  1236. }
  1237. s->dct_count[intra] >>= 1;
  1238. }
  1239. for(i=0; i<64; i++){
  1240. 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);
  1241. }
  1242. }
  1243. }
  1244. /**
  1245. * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
  1246. */
  1247. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1248. {
  1249. int i;
  1250. AVFrame *pic;
  1251. s->mb_skiped = 0;
  1252. assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
  1253. /* mark&release old frames */
  1254. if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr->data[0]) {
  1255. avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
  1256. /* release forgotten pictures */
  1257. /* if(mpeg124/h263) */
  1258. if(!s->encoding){
  1259. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1260. if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
  1261. av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
  1262. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  1263. }
  1264. }
  1265. }
  1266. }
  1267. alloc:
  1268. if(!s->encoding){
  1269. /* release non refernce frames */
  1270. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1271. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  1272. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  1273. }
  1274. }
  1275. if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
  1276. pic= (AVFrame*)s->current_picture_ptr; //we allready have a unused image (maybe it was set before reading the header)
  1277. else{
  1278. i= ff_find_unused_picture(s, 0);
  1279. pic= (AVFrame*)&s->picture[i];
  1280. }
  1281. pic->reference= s->pict_type != B_TYPE && !s->dropable ? 3 : 0;
  1282. pic->coded_picture_number= s->coded_picture_number++;
  1283. if( alloc_picture(s, (Picture*)pic, 0) < 0)
  1284. return -1;
  1285. s->current_picture_ptr= (Picture*)pic;
  1286. s->current_picture_ptr->top_field_first= s->top_field_first; //FIXME use only the vars from current_pic
  1287. s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
  1288. }
  1289. s->current_picture_ptr->pict_type= s->pict_type;
  1290. // if(s->flags && CODEC_FLAG_QSCALE)
  1291. // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
  1292. s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
  1293. copy_picture(&s->current_picture, s->current_picture_ptr);
  1294. if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
  1295. if (s->pict_type != B_TYPE) {
  1296. s->last_picture_ptr= s->next_picture_ptr;
  1297. if(!s->dropable)
  1298. s->next_picture_ptr= s->current_picture_ptr;
  1299. }
  1300. /* 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,
  1301. s->last_picture_ptr ? s->last_picture_ptr->data[0] : NULL,
  1302. s->next_picture_ptr ? s->next_picture_ptr->data[0] : NULL,
  1303. s->current_picture_ptr ? s->current_picture_ptr->data[0] : NULL,
  1304. s->pict_type, s->dropable);*/
  1305. if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
  1306. if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
  1307. if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){
  1308. av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
  1309. assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
  1310. goto alloc;
  1311. }
  1312. assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
  1313. if(s->picture_structure!=PICT_FRAME){
  1314. int i;
  1315. for(i=0; i<4; i++){
  1316. if(s->picture_structure == PICT_BOTTOM_FIELD){
  1317. s->current_picture.data[i] += s->current_picture.linesize[i];
  1318. }
  1319. s->current_picture.linesize[i] *= 2;
  1320. s->last_picture.linesize[i] *=2;
  1321. s->next_picture.linesize[i] *=2;
  1322. }
  1323. }
  1324. }
  1325. s->hurry_up= s->avctx->hurry_up;
  1326. s->error_resilience= avctx->error_resilience;
  1327. /* set dequantizer, we cant do it during init as it might change for mpeg4
  1328. and we cant do it in the header decode as init isnt called for mpeg4 there yet */
  1329. if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
  1330. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1331. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1332. }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  1333. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1334. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1335. }else{
  1336. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1337. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1338. }
  1339. if(s->dct_error_sum){
  1340. assert(s->avctx->noise_reduction && s->encoding);
  1341. update_noise_reduction(s);
  1342. }
  1343. #ifdef HAVE_XVMC
  1344. if(s->avctx->xvmc_acceleration)
  1345. return XVMC_field_start(s, avctx);
  1346. #endif
  1347. return 0;
  1348. }
  1349. /* generic function for encode/decode called after a frame has been coded/decoded */
  1350. void MPV_frame_end(MpegEncContext *s)
  1351. {
  1352. int i;
  1353. /* draw edge for correct motion prediction if outside */
  1354. #ifdef HAVE_XVMC
  1355. //just to make sure that all data is rendered.
  1356. if(s->avctx->xvmc_acceleration){
  1357. XVMC_field_end(s);
  1358. }else
  1359. #endif
  1360. if(s->unrestricted_mv && s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  1361. draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
  1362. draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  1363. draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  1364. }
  1365. emms_c();
  1366. s->last_pict_type = s->pict_type;
  1367. if(s->pict_type!=B_TYPE){
  1368. s->last_non_b_pict_type= s->pict_type;
  1369. }
  1370. #if 0
  1371. /* copy back current_picture variables */
  1372. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1373. if(s->picture[i].data[0] == s->current_picture.data[0]){
  1374. s->picture[i]= s->current_picture;
  1375. break;
  1376. }
  1377. }
  1378. assert(i<MAX_PICTURE_COUNT);
  1379. #endif
  1380. if(s->encoding){
  1381. /* release non refernce frames */
  1382. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1383. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  1384. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  1385. }
  1386. }
  1387. }
  1388. // clear copies, to avoid confusion
  1389. #if 0
  1390. memset(&s->last_picture, 0, sizeof(Picture));
  1391. memset(&s->next_picture, 0, sizeof(Picture));
  1392. memset(&s->current_picture, 0, sizeof(Picture));
  1393. #endif
  1394. s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
  1395. }
  1396. /**
  1397. * draws an line from (ex, ey) -> (sx, sy).
  1398. * @param w width of the image
  1399. * @param h height of the image
  1400. * @param stride stride/linesize of the image
  1401. * @param color color of the arrow
  1402. */
  1403. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1404. int t, x, y, fr, f;
  1405. sx= clip(sx, 0, w-1);
  1406. sy= clip(sy, 0, h-1);
  1407. ex= clip(ex, 0, w-1);
  1408. ey= clip(ey, 0, h-1);
  1409. buf[sy*stride + sx]+= color;
  1410. if(ABS(ex - sx) > ABS(ey - sy)){
  1411. if(sx > ex){
  1412. t=sx; sx=ex; ex=t;
  1413. t=sy; sy=ey; ey=t;
  1414. }
  1415. buf+= sx + sy*stride;
  1416. ex-= sx;
  1417. f= ((ey-sy)<<16)/ex;
  1418. for(x= 0; x <= ex; x++){
  1419. y = (x*f)>>16;
  1420. fr= (x*f)&0xFFFF;
  1421. buf[ y *stride + x]+= (color*(0x10000-fr))>>16;
  1422. buf[(y+1)*stride + x]+= (color* fr )>>16;
  1423. }
  1424. }else{
  1425. if(sy > ey){
  1426. t=sx; sx=ex; ex=t;
  1427. t=sy; sy=ey; ey=t;
  1428. }
  1429. buf+= sx + sy*stride;
  1430. ey-= sy;
  1431. if(ey) f= ((ex-sx)<<16)/ey;
  1432. else f= 0;
  1433. for(y= 0; y <= ey; y++){
  1434. x = (y*f)>>16;
  1435. fr= (y*f)&0xFFFF;
  1436. buf[y*stride + x ]+= (color*(0x10000-fr))>>16;;
  1437. buf[y*stride + x+1]+= (color* fr )>>16;;
  1438. }
  1439. }
  1440. }
  1441. /**
  1442. * draws an arrow from (ex, ey) -> (sx, sy).
  1443. * @param w width of the image
  1444. * @param h height of the image
  1445. * @param stride stride/linesize of the image
  1446. * @param color color of the arrow
  1447. */
  1448. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1449. int dx,dy;
  1450. sx= clip(sx, -100, w+100);
  1451. sy= clip(sy, -100, h+100);
  1452. ex= clip(ex, -100, w+100);
  1453. ey= clip(ey, -100, h+100);
  1454. dx= ex - sx;
  1455. dy= ey - sy;
  1456. if(dx*dx + dy*dy > 3*3){
  1457. int rx= dx + dy;
  1458. int ry= -dx + dy;
  1459. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  1460. //FIXME subpixel accuracy
  1461. rx= ROUNDED_DIV(rx*3<<4, length);
  1462. ry= ROUNDED_DIV(ry*3<<4, length);
  1463. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1464. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1465. }
  1466. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1467. }
  1468. /**
  1469. * prints debuging info for the given picture.
  1470. */
  1471. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
  1472. if(!pict || !pict->mb_type) return;
  1473. if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
  1474. int x,y;
  1475. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1476. switch (pict->pict_type) {
  1477. case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
  1478. case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
  1479. case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
  1480. case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
  1481. case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
  1482. case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;
  1483. }
  1484. for(y=0; y<s->mb_height; y++){
  1485. for(x=0; x<s->mb_width; x++){
  1486. if(s->avctx->debug&FF_DEBUG_SKIP){
  1487. int count= s->mbskip_table[x + y*s->mb_stride];
  1488. if(count>9) count=9;
  1489. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1490. }
  1491. if(s->avctx->debug&FF_DEBUG_QP){
  1492. av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
  1493. }
  1494. if(s->avctx->debug&FF_DEBUG_MB_TYPE){
  1495. int mb_type= pict->mb_type[x + y*s->mb_stride];
  1496. //Type & MV direction
  1497. if(IS_PCM(mb_type))
  1498. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1499. else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1500. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1501. else if(IS_INTRA4x4(mb_type))
  1502. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1503. else if(IS_INTRA16x16(mb_type))
  1504. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1505. else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1506. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1507. else if(IS_DIRECT(mb_type))
  1508. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1509. else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
  1510. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1511. else if(IS_GMC(mb_type))
  1512. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1513. else if(IS_SKIP(mb_type))
  1514. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1515. else if(!USES_LIST(mb_type, 1))
  1516. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1517. else if(!USES_LIST(mb_type, 0))
  1518. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1519. else{
  1520. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1521. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1522. }
  1523. //segmentation
  1524. if(IS_8X8(mb_type))
  1525. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1526. else if(IS_16X8(mb_type))
  1527. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1528. else if(IS_8X16(mb_type))
  1529. av_log(s->avctx, AV_LOG_DEBUG, "¦");
  1530. else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
  1531. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1532. else
  1533. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1534. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
  1535. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1536. else
  1537. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1538. }
  1539. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1540. }
  1541. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1542. }
  1543. }
  1544. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  1545. const int shift= 1 + s->quarter_sample;
  1546. int mb_y;
  1547. uint8_t *ptr;
  1548. int i;
  1549. int h_chroma_shift, v_chroma_shift;
  1550. const int width = s->avctx->width;
  1551. const int height= s->avctx->height;
  1552. const int mv_sample_log2= 4 - pict->motion_subsample_log2;
  1553. const int mv_stride= (s->mb_width << mv_sample_log2) + 1;
  1554. s->low_delay=0; //needed to see the vectors without trashing the buffers
  1555. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1556. for(i=0; i<3; i++){
  1557. memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*height:pict->linesize[i]*height >> v_chroma_shift);
  1558. pict->data[i]= s->visualization_buffer[i];
  1559. }
  1560. pict->type= FF_BUFFER_TYPE_COPY;
  1561. ptr= pict->data[0];
  1562. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  1563. int mb_x;
  1564. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  1565. const int mb_index= mb_x + mb_y*s->mb_stride;
  1566. if((s->avctx->debug_mv) && pict->motion_val){
  1567. int type;
  1568. for(type=0; type<3; type++){
  1569. int direction = 0;
  1570. switch (type) {
  1571. case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=FF_P_TYPE))
  1572. continue;
  1573. direction = 0;
  1574. break;
  1575. case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=FF_B_TYPE))
  1576. continue;
  1577. direction = 0;
  1578. break;
  1579. case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=FF_B_TYPE))
  1580. continue;
  1581. direction = 1;
  1582. break;
  1583. }
  1584. if(!USES_LIST(pict->mb_type[mb_index], direction))
  1585. continue;
  1586. if(IS_8X8(pict->mb_type[mb_index])){
  1587. int i;
  1588. for(i=0; i<4; i++){
  1589. int sx= mb_x*16 + 4 + 8*(i&1);
  1590. int sy= mb_y*16 + 4 + 8*(i>>1);
  1591. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << mv_sample_log2-1;
  1592. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1593. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1594. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1595. }
  1596. }else if(IS_16X8(pict->mb_type[mb_index])){
  1597. int i;
  1598. for(i=0; i<2; i++){
  1599. int sx=mb_x*16 + 8;
  1600. int sy=mb_y*16 + 4 + 8*i;
  1601. int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << mv_sample_log2-1;
  1602. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1603. int my=(pict->motion_val[direction][xy][1]>>shift);
  1604. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1605. my*=2;
  1606. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1607. }
  1608. }else if(IS_8X16(pict->mb_type[mb_index])){
  1609. int i;
  1610. for(i=0; i<2; i++){
  1611. int sx=mb_x*16 + 4 + 8*i;
  1612. int sy=mb_y*16 + 8;
  1613. int xy= (mb_x*2 + i + mb_y*2*mv_stride) << mv_sample_log2-1;
  1614. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1615. int my=(pict->motion_val[direction][xy][1]>>shift);
  1616. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1617. my*=2;
  1618. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1619. }
  1620. }else{
  1621. int sx= mb_x*16 + 8;
  1622. int sy= mb_y*16 + 8;
  1623. int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2;
  1624. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1625. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1626. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1627. }
  1628. }
  1629. }
  1630. if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
  1631. uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
  1632. int y;
  1633. for(y=0; y<8; y++){
  1634. *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= c;
  1635. *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= c;
  1636. }
  1637. }
  1638. if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
  1639. int mb_type= pict->mb_type[mb_index];
  1640. uint64_t u,v;
  1641. int y;
  1642. #define COLOR(theta, r)\
  1643. u= (int)(128 + r*cos(theta*3.141592/180));\
  1644. v= (int)(128 + r*sin(theta*3.141592/180));
  1645. u=v=128;
  1646. if(IS_PCM(mb_type)){
  1647. COLOR(120,48)
  1648. }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
  1649. COLOR(30,48)
  1650. }else if(IS_INTRA4x4(mb_type)){
  1651. COLOR(90,48)
  1652. }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
  1653. // COLOR(120,48)
  1654. }else if(IS_DIRECT(mb_type)){
  1655. COLOR(150,48)
  1656. }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
  1657. COLOR(170,48)
  1658. }else if(IS_GMC(mb_type)){
  1659. COLOR(190,48)
  1660. }else if(IS_SKIP(mb_type)){
  1661. // COLOR(180,48)
  1662. }else if(!USES_LIST(mb_type, 1)){
  1663. COLOR(240,48)
  1664. }else if(!USES_LIST(mb_type, 0)){
  1665. COLOR(0,48)
  1666. }else{
  1667. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1668. COLOR(300,48)
  1669. }
  1670. u*= 0x0101010101010101ULL;
  1671. v*= 0x0101010101010101ULL;
  1672. for(y=0; y<8; y++){
  1673. *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= u;
  1674. *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= v;
  1675. }
  1676. //segmentation
  1677. if(IS_8X8(mb_type) || IS_16X8(mb_type)){
  1678. *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1679. *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1680. }
  1681. if(IS_8X8(mb_type) || IS_8X16(mb_type)){
  1682. for(y=0; y<16; y++)
  1683. pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
  1684. }
  1685. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
  1686. // hmm
  1687. }
  1688. }
  1689. s->mbskip_table[mb_index]=0;
  1690. }
  1691. }
  1692. }
  1693. }
  1694. #ifdef CONFIG_ENCODERS
  1695. static int get_sae(uint8_t *src, int ref, int stride){
  1696. int x,y;
  1697. int acc=0;
  1698. for(y=0; y<16; y++){
  1699. for(x=0; x<16; x++){
  1700. acc+= ABS(src[x+y*stride] - ref);
  1701. }
  1702. }
  1703. return acc;
  1704. }
  1705. static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
  1706. int x, y, w, h;
  1707. int acc=0;
  1708. w= s->width &~15;
  1709. h= s->height&~15;
  1710. for(y=0; y<h; y+=16){
  1711. for(x=0; x<w; x+=16){
  1712. int offset= x + y*stride;
  1713. int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride, 16);
  1714. int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
  1715. int sae = get_sae(src + offset, mean, stride);
  1716. acc+= sae + 500 < sad;
  1717. }
  1718. }
  1719. return acc;
  1720. }
  1721. static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
  1722. AVFrame *pic=NULL;
  1723. int64_t pts;
  1724. int i;
  1725. const int encoding_delay= s->max_b_frames;
  1726. int direct=1;
  1727. if(pic_arg){
  1728. pts= pic_arg->pts;
  1729. pic_arg->display_picture_number= s->input_picture_number++;
  1730. if(pts != AV_NOPTS_VALUE){
  1731. if(s->user_specified_pts != AV_NOPTS_VALUE){
  1732. int64_t time= av_rescale(pts, s->avctx->frame_rate, s->avctx->frame_rate_base*(int64_t)AV_TIME_BASE);
  1733. int64_t last= av_rescale(s->user_specified_pts, s->avctx->frame_rate, s->avctx->frame_rate_base*(int64_t)AV_TIME_BASE);
  1734. if(time <= last){
  1735. av_log(s->avctx, AV_LOG_ERROR, "Error, Invalid timestamp=%Ld, last=%Ld\n", pts, s->user_specified_pts);
  1736. return -1;
  1737. }
  1738. }
  1739. s->user_specified_pts= pts;
  1740. }else{
  1741. if(s->user_specified_pts != AV_NOPTS_VALUE){
  1742. s->user_specified_pts=
  1743. pts= s->user_specified_pts + AV_TIME_BASE*(int64_t)s->avctx->frame_rate_base / s->avctx->frame_rate;
  1744. av_log(s->avctx, AV_LOG_INFO, "Warning: AVFrame.pts=? trying to guess (%Ld)\n", pts);
  1745. }else{
  1746. pts= av_rescale(pic_arg->display_picture_number*(int64_t)s->avctx->frame_rate_base, AV_TIME_BASE, s->avctx->frame_rate);
  1747. }
  1748. }
  1749. }
  1750. if(pic_arg){
  1751. if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
  1752. if(pic_arg->linesize[0] != s->linesize) direct=0;
  1753. if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
  1754. if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
  1755. // av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
  1756. if(direct){
  1757. i= ff_find_unused_picture(s, 1);
  1758. pic= (AVFrame*)&s->picture[i];
  1759. pic->reference= 3;
  1760. for(i=0; i<4; i++){
  1761. pic->data[i]= pic_arg->data[i];
  1762. pic->linesize[i]= pic_arg->linesize[i];
  1763. }
  1764. alloc_picture(s, (Picture*)pic, 1);
  1765. }else{
  1766. int offset= 16;
  1767. i= ff_find_unused_picture(s, 0);
  1768. pic= (AVFrame*)&s->picture[i];
  1769. pic->reference= 3;
  1770. alloc_picture(s, (Picture*)pic, 0);
  1771. if( pic->data[0] + offset == pic_arg->data[0]
  1772. && pic->data[1] + offset == pic_arg->data[1]
  1773. && pic->data[2] + offset == pic_arg->data[2]){
  1774. // empty
  1775. }else{
  1776. int h_chroma_shift, v_chroma_shift;
  1777. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1778. for(i=0; i<3; i++){
  1779. int src_stride= pic_arg->linesize[i];
  1780. int dst_stride= i ? s->uvlinesize : s->linesize;
  1781. int h_shift= i ? h_chroma_shift : 0;
  1782. int v_shift= i ? v_chroma_shift : 0;
  1783. int w= s->width >>h_shift;
  1784. int h= s->height>>v_shift;
  1785. uint8_t *src= pic_arg->data[i];
  1786. uint8_t *dst= pic->data[i] + offset;
  1787. if(src_stride==dst_stride)
  1788. memcpy(dst, src, src_stride*h);
  1789. else{
  1790. while(h--){
  1791. memcpy(dst, src, w);
  1792. dst += dst_stride;
  1793. src += src_stride;
  1794. }
  1795. }
  1796. }
  1797. }
  1798. }
  1799. copy_picture_attributes(s, pic, pic_arg);
  1800. pic->pts= pts; //we set this here to avoid modifiying pic_arg
  1801. }
  1802. /* shift buffer entries */
  1803. for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
  1804. s->input_picture[i-1]= s->input_picture[i];
  1805. s->input_picture[encoding_delay]= (Picture*)pic;
  1806. return 0;
  1807. }
  1808. static int skip_check(MpegEncContext *s, Picture *p, Picture *ref){
  1809. int x, y, plane;
  1810. int score=0;
  1811. int64_t score64=0;
  1812. int64_t threshold;
  1813. for(plane=0; plane<3; plane++){
  1814. const int stride= p->linesize[plane];
  1815. const int bw= plane ? 1 : 2;
  1816. for(y=0; y<s->mb_height*bw; y++){
  1817. for(x=0; x<s->mb_width*bw; x++){
  1818. int v= s->dsp.frame_skip_cmp[1](s, p->data[plane] + 8*(x + y*stride), ref->data[plane] + 8*(x + y*stride), stride, 8);
  1819. switch(s->avctx->frame_skip_exp){
  1820. case 0: score= FFMAX(score, v); break;
  1821. case 1: score+= ABS(v);break;
  1822. case 2: score+= v*v;break;
  1823. case 3: score64+= ABS(v*v*(int64_t)v);break;
  1824. case 4: score64+= v*v*(int64_t)(v*v);break;
  1825. }
  1826. }
  1827. }
  1828. }
  1829. if(score) score64= score;
  1830. if(score64 < s->avctx->frame_skip_threshold)
  1831. return 1;
  1832. if(score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda)>>8))
  1833. return 1;
  1834. return 0;
  1835. }
  1836. static void select_input_picture(MpegEncContext *s){
  1837. int i;
  1838. for(i=1; i<MAX_PICTURE_COUNT; i++)
  1839. s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
  1840. s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
  1841. /* set next picture types & ordering */
  1842. if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
  1843. if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
  1844. s->reordered_input_picture[0]= s->input_picture[0];
  1845. s->reordered_input_picture[0]->pict_type= I_TYPE;
  1846. s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
  1847. }else{
  1848. int b_frames;
  1849. if(s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor){
  1850. if(skip_check(s, s->input_picture[0], s->next_picture_ptr)){
  1851. //av_log(NULL, AV_LOG_DEBUG, "skip %p %Ld\n", s->input_picture[0]->data[0], s->input_picture[0]->pts);
  1852. if(s->input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
  1853. for(i=0; i<4; i++)
  1854. s->input_picture[0]->data[i]= NULL;
  1855. s->input_picture[0]->type= 0;
  1856. }else{
  1857. assert( s->input_picture[0]->type==FF_BUFFER_TYPE_USER
  1858. || s->input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
  1859. s->avctx->release_buffer(s->avctx, (AVFrame*)s->input_picture[0]);
  1860. }
  1861. goto no_output_pic;
  1862. }
  1863. }
  1864. if(s->flags&CODEC_FLAG_PASS2){
  1865. for(i=0; i<s->max_b_frames+1; i++){
  1866. int pict_num= s->input_picture[0]->display_picture_number + i;
  1867. if(pict_num >= s->rc_context.num_entries)
  1868. break;
  1869. if(!s->input_picture[i]){
  1870. s->rc_context.entry[pict_num-1].new_pict_type = P_TYPE;
  1871. break;
  1872. }
  1873. s->input_picture[i]->pict_type=
  1874. s->rc_context.entry[pict_num].new_pict_type;
  1875. }
  1876. }
  1877. if(s->avctx->b_frame_strategy==0){
  1878. b_frames= s->max_b_frames;
  1879. while(b_frames && !s->input_picture[b_frames]) b_frames--;
  1880. }else if(s->avctx->b_frame_strategy==1){
  1881. for(i=1; i<s->max_b_frames+1; i++){
  1882. if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
  1883. s->input_picture[i]->b_frame_score=
  1884. get_intra_count(s, s->input_picture[i ]->data[0],
  1885. s->input_picture[i-1]->data[0], s->linesize) + 1;
  1886. }
  1887. }
  1888. for(i=0; i<s->max_b_frames; i++){
  1889. if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
  1890. }
  1891. b_frames= FFMAX(0, i-1);
  1892. /* reset scores */
  1893. for(i=0; i<b_frames+1; i++){
  1894. s->input_picture[i]->b_frame_score=0;
  1895. }
  1896. }else{
  1897. av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
  1898. b_frames=0;
  1899. }
  1900. emms_c();
  1901. //static int b_count=0;
  1902. //b_count+= b_frames;
  1903. //av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
  1904. for(i= b_frames - 1; i>=0; i--){
  1905. int type= s->input_picture[i]->pict_type;
  1906. if(type && type != B_TYPE)
  1907. b_frames= i;
  1908. }
  1909. if(s->input_picture[b_frames]->pict_type == B_TYPE && b_frames == s->max_b_frames){
  1910. av_log(s->avctx, AV_LOG_ERROR, "warning, too many bframes in a row\n");
  1911. }
  1912. if(s->picture_in_gop_number + b_frames >= s->gop_size){
  1913. if((s->flags2 & CODEC_FLAG2_STRICT_GOP) && s->gop_size > s->picture_in_gop_number){
  1914. b_frames= s->gop_size - s->picture_in_gop_number - 1;
  1915. }else{
  1916. if(s->flags & CODEC_FLAG_CLOSED_GOP)
  1917. b_frames=0;
  1918. s->input_picture[b_frames]->pict_type= I_TYPE;
  1919. }
  1920. }
  1921. if( (s->flags & CODEC_FLAG_CLOSED_GOP)
  1922. && b_frames
  1923. && s->input_picture[b_frames]->pict_type== I_TYPE)
  1924. b_frames--;
  1925. s->reordered_input_picture[0]= s->input_picture[b_frames];
  1926. if(s->reordered_input_picture[0]->pict_type != I_TYPE)
  1927. s->reordered_input_picture[0]->pict_type= P_TYPE;
  1928. s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
  1929. for(i=0; i<b_frames; i++){
  1930. s->reordered_input_picture[i+1]= s->input_picture[i];
  1931. s->reordered_input_picture[i+1]->pict_type= B_TYPE;
  1932. s->reordered_input_picture[i+1]->coded_picture_number= s->coded_picture_number++;
  1933. }
  1934. }
  1935. }
  1936. no_output_pic:
  1937. if(s->reordered_input_picture[0]){
  1938. s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
  1939. copy_picture(&s->new_picture, s->reordered_input_picture[0]);
  1940. if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
  1941. // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
  1942. int i= ff_find_unused_picture(s, 0);
  1943. Picture *pic= &s->picture[i];
  1944. /* mark us unused / free shared pic */
  1945. for(i=0; i<4; i++)
  1946. s->reordered_input_picture[0]->data[i]= NULL;
  1947. s->reordered_input_picture[0]->type= 0;
  1948. pic->reference = s->reordered_input_picture[0]->reference;
  1949. alloc_picture(s, pic, 0);
  1950. copy_picture_attributes(s, (AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]);
  1951. s->current_picture_ptr= pic;
  1952. }else{
  1953. // input is not a shared pix -> reuse buffer for current_pix
  1954. assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
  1955. || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
  1956. s->current_picture_ptr= s->reordered_input_picture[0];
  1957. for(i=0; i<4; i++){
  1958. s->new_picture.data[i]+=16;
  1959. }
  1960. }
  1961. copy_picture(&s->current_picture, s->current_picture_ptr);
  1962. s->picture_number= s->new_picture.display_picture_number;
  1963. //printf("dpn:%d\n", s->picture_number);
  1964. }else{
  1965. memset(&s->new_picture, 0, sizeof(Picture));
  1966. }
  1967. }
  1968. int MPV_encode_picture(AVCodecContext *avctx,
  1969. unsigned char *buf, int buf_size, void *data)
  1970. {
  1971. MpegEncContext *s = avctx->priv_data;
  1972. AVFrame *pic_arg = data;
  1973. int i, stuffing_count;
  1974. if(avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUVJ420P){
  1975. av_log(avctx, AV_LOG_ERROR, "this codec supports only YUV420P\n");
  1976. return -1;
  1977. }
  1978. for(i=0; i<avctx->thread_count; i++){
  1979. int start_y= s->thread_context[i]->start_mb_y;
  1980. int end_y= s->thread_context[i]-> end_mb_y;
  1981. int h= s->mb_height;
  1982. uint8_t *start= buf + buf_size*start_y/h;
  1983. uint8_t *end = buf + buf_size* end_y/h;
  1984. init_put_bits(&s->thread_context[i]->pb, start, end - start);
  1985. }
  1986. s->picture_in_gop_number++;
  1987. if(load_input_picture(s, pic_arg) < 0)
  1988. return -1;
  1989. select_input_picture(s);
  1990. /* output? */
  1991. if(s->new_picture.data[0]){
  1992. s->pict_type= s->new_picture.pict_type;
  1993. //emms_c();
  1994. //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
  1995. MPV_frame_start(s, avctx);
  1996. encode_picture(s, s->picture_number);
  1997. avctx->real_pict_num = s->picture_number;
  1998. avctx->header_bits = s->header_bits;
  1999. avctx->mv_bits = s->mv_bits;
  2000. avctx->misc_bits = s->misc_bits;
  2001. avctx->i_tex_bits = s->i_tex_bits;
  2002. avctx->p_tex_bits = s->p_tex_bits;
  2003. avctx->i_count = s->i_count;
  2004. avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
  2005. avctx->skip_count = s->skip_count;
  2006. MPV_frame_end(s);
  2007. if (s->out_format == FMT_MJPEG)
  2008. mjpeg_picture_trailer(s);
  2009. if(s->flags&CODEC_FLAG_PASS1)
  2010. ff_write_pass1_stats(s);
  2011. for(i=0; i<4; i++){
  2012. avctx->error[i] += s->current_picture_ptr->error[i];
  2013. }
  2014. if(s->flags&CODEC_FLAG_PASS1)
  2015. assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits + avctx->i_tex_bits + avctx->p_tex_bits == put_bits_count(&s->pb));
  2016. flush_put_bits(&s->pb);
  2017. s->frame_bits = put_bits_count(&s->pb);
  2018. stuffing_count= ff_vbv_update(s, s->frame_bits);
  2019. if(stuffing_count){
  2020. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < stuffing_count + 50){
  2021. av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
  2022. return -1;
  2023. }
  2024. switch(s->codec_id){
  2025. case CODEC_ID_MPEG1VIDEO:
  2026. case CODEC_ID_MPEG2VIDEO:
  2027. while(stuffing_count--){
  2028. put_bits(&s->pb, 8, 0);
  2029. }
  2030. break;
  2031. case CODEC_ID_MPEG4:
  2032. put_bits(&s->pb, 16, 0);
  2033. put_bits(&s->pb, 16, 0x1C3);
  2034. stuffing_count -= 4;
  2035. while(stuffing_count--){
  2036. put_bits(&s->pb, 8, 0xFF);
  2037. }
  2038. break;
  2039. default:
  2040. av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
  2041. }
  2042. flush_put_bits(&s->pb);
  2043. s->frame_bits = put_bits_count(&s->pb);
  2044. }
  2045. /* update mpeg1/2 vbv_delay for CBR */
  2046. if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate && s->out_format == FMT_MPEG1
  2047. && 90000LL * (avctx->rc_buffer_size-1) <= s->avctx->rc_max_rate*0xFFFFLL){
  2048. int vbv_delay;
  2049. assert(s->repeat_first_field==0);
  2050. vbv_delay= lrintf(90000 * s->rc_context.buffer_index / s->avctx->rc_max_rate);
  2051. assert(vbv_delay < 0xFFFF);
  2052. s->vbv_delay_ptr[0] &= 0xF8;
  2053. s->vbv_delay_ptr[0] |= vbv_delay>>13;
  2054. s->vbv_delay_ptr[1] = vbv_delay>>5;
  2055. s->vbv_delay_ptr[2] &= 0x07;
  2056. s->vbv_delay_ptr[2] |= vbv_delay<<3;
  2057. }
  2058. s->total_bits += s->frame_bits;
  2059. avctx->frame_bits = s->frame_bits;
  2060. }else{
  2061. assert((pbBufPtr(&s->pb) == s->pb.buf));
  2062. s->frame_bits=0;
  2063. }
  2064. assert((s->frame_bits&7)==0);
  2065. return s->frame_bits/8;
  2066. }
  2067. #endif //CONFIG_ENCODERS
  2068. static inline void gmc1_motion(MpegEncContext *s,
  2069. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2070. uint8_t **ref_picture)
  2071. {
  2072. uint8_t *ptr;
  2073. int offset, src_x, src_y, linesize, uvlinesize;
  2074. int motion_x, motion_y;
  2075. int emu=0;
  2076. motion_x= s->sprite_offset[0][0];
  2077. motion_y= s->sprite_offset[0][1];
  2078. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  2079. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  2080. motion_x<<=(3-s->sprite_warping_accuracy);
  2081. motion_y<<=(3-s->sprite_warping_accuracy);
  2082. src_x = clip(src_x, -16, s->width);
  2083. if (src_x == s->width)
  2084. motion_x =0;
  2085. src_y = clip(src_y, -16, s->height);
  2086. if (src_y == s->height)
  2087. motion_y =0;
  2088. linesize = s->linesize;
  2089. uvlinesize = s->uvlinesize;
  2090. ptr = ref_picture[0] + (src_y * linesize) + src_x;
  2091. if(s->flags&CODEC_FLAG_EMU_EDGE){
  2092. if( (unsigned)src_x >= s->h_edge_pos - 17
  2093. || (unsigned)src_y >= s->v_edge_pos - 17){
  2094. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  2095. ptr= s->edge_emu_buffer;
  2096. }
  2097. }
  2098. if((motion_x|motion_y)&7){
  2099. s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  2100. s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  2101. }else{
  2102. int dxy;
  2103. dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
  2104. if (s->no_rounding){
  2105. s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  2106. }else{
  2107. s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
  2108. }
  2109. }
  2110. if(s->flags&CODEC_FLAG_GRAY) return;
  2111. motion_x= s->sprite_offset[1][0];
  2112. motion_y= s->sprite_offset[1][1];
  2113. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  2114. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  2115. motion_x<<=(3-s->sprite_warping_accuracy);
  2116. motion_y<<=(3-s->sprite_warping_accuracy);
  2117. src_x = clip(src_x, -8, s->width>>1);
  2118. if (src_x == s->width>>1)
  2119. motion_x =0;
  2120. src_y = clip(src_y, -8, s->height>>1);
  2121. if (src_y == s->height>>1)
  2122. motion_y =0;
  2123. offset = (src_y * uvlinesize) + src_x;
  2124. ptr = ref_picture[1] + offset;
  2125. if(s->flags&CODEC_FLAG_EMU_EDGE){
  2126. if( (unsigned)src_x >= (s->h_edge_pos>>1) - 9
  2127. || (unsigned)src_y >= (s->v_edge_pos>>1) - 9){
  2128. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2129. ptr= s->edge_emu_buffer;
  2130. emu=1;
  2131. }
  2132. }
  2133. s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  2134. ptr = ref_picture[2] + offset;
  2135. if(emu){
  2136. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2137. ptr= s->edge_emu_buffer;
  2138. }
  2139. s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  2140. return;
  2141. }
  2142. static inline void gmc_motion(MpegEncContext *s,
  2143. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2144. uint8_t **ref_picture)
  2145. {
  2146. uint8_t *ptr;
  2147. int linesize, uvlinesize;
  2148. const int a= s->sprite_warping_accuracy;
  2149. int ox, oy;
  2150. linesize = s->linesize;
  2151. uvlinesize = s->uvlinesize;
  2152. ptr = ref_picture[0];
  2153. ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
  2154. oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
  2155. s->dsp.gmc(dest_y, ptr, linesize, 16,
  2156. ox,
  2157. oy,
  2158. s->sprite_delta[0][0], s->sprite_delta[0][1],
  2159. s->sprite_delta[1][0], s->sprite_delta[1][1],
  2160. a+1, (1<<(2*a+1)) - s->no_rounding,
  2161. s->h_edge_pos, s->v_edge_pos);
  2162. s->dsp.gmc(dest_y+8, ptr, linesize, 16,
  2163. ox + s->sprite_delta[0][0]*8,
  2164. oy + s->sprite_delta[1][0]*8,
  2165. s->sprite_delta[0][0], s->sprite_delta[0][1],
  2166. s->sprite_delta[1][0], s->sprite_delta[1][1],
  2167. a+1, (1<<(2*a+1)) - s->no_rounding,
  2168. s->h_edge_pos, s->v_edge_pos);
  2169. if(s->flags&CODEC_FLAG_GRAY) return;
  2170. ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
  2171. oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
  2172. ptr = ref_picture[1];
  2173. s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
  2174. ox,
  2175. oy,
  2176. s->sprite_delta[0][0], s->sprite_delta[0][1],
  2177. s->sprite_delta[1][0], s->sprite_delta[1][1],
  2178. a+1, (1<<(2*a+1)) - s->no_rounding,
  2179. s->h_edge_pos>>1, s->v_edge_pos>>1);
  2180. ptr = ref_picture[2];
  2181. s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
  2182. ox,
  2183. oy,
  2184. s->sprite_delta[0][0], s->sprite_delta[0][1],
  2185. s->sprite_delta[1][0], s->sprite_delta[1][1],
  2186. a+1, (1<<(2*a+1)) - s->no_rounding,
  2187. s->h_edge_pos>>1, s->v_edge_pos>>1);
  2188. }
  2189. /**
  2190. * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples.
  2191. * @param buf destination buffer
  2192. * @param src source buffer
  2193. * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers
  2194. * @param block_w width of block
  2195. * @param block_h height of block
  2196. * @param src_x x coordinate of the top left sample of the block in the source buffer
  2197. * @param src_y y coordinate of the top left sample of the block in the source buffer
  2198. * @param w width of the source buffer
  2199. * @param h height of the source buffer
  2200. */
  2201. void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
  2202. int src_x, int src_y, int w, int h){
  2203. int x, y;
  2204. int start_y, start_x, end_y, end_x;
  2205. if(src_y>= h){
  2206. src+= (h-1-src_y)*linesize;
  2207. src_y=h-1;
  2208. }else if(src_y<=-block_h){
  2209. src+= (1-block_h-src_y)*linesize;
  2210. src_y=1-block_h;
  2211. }
  2212. if(src_x>= w){
  2213. src+= (w-1-src_x);
  2214. src_x=w-1;
  2215. }else if(src_x<=-block_w){
  2216. src+= (1-block_w-src_x);
  2217. src_x=1-block_w;
  2218. }
  2219. start_y= FFMAX(0, -src_y);
  2220. start_x= FFMAX(0, -src_x);
  2221. end_y= FFMIN(block_h, h-src_y);
  2222. end_x= FFMIN(block_w, w-src_x);
  2223. // copy existing part
  2224. for(y=start_y; y<end_y; y++){
  2225. for(x=start_x; x<end_x; x++){
  2226. buf[x + y*linesize]= src[x + y*linesize];
  2227. }
  2228. }
  2229. //top
  2230. for(y=0; y<start_y; y++){
  2231. for(x=start_x; x<end_x; x++){
  2232. buf[x + y*linesize]= buf[x + start_y*linesize];
  2233. }
  2234. }
  2235. //bottom
  2236. for(y=end_y; y<block_h; y++){
  2237. for(x=start_x; x<end_x; x++){
  2238. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  2239. }
  2240. }
  2241. for(y=0; y<block_h; y++){
  2242. //left
  2243. for(x=0; x<start_x; x++){
  2244. buf[x + y*linesize]= buf[start_x + y*linesize];
  2245. }
  2246. //right
  2247. for(x=end_x; x<block_w; x++){
  2248. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  2249. }
  2250. }
  2251. }
  2252. static inline int hpel_motion(MpegEncContext *s,
  2253. uint8_t *dest, uint8_t *src,
  2254. int field_based, int field_select,
  2255. int src_x, int src_y,
  2256. int width, int height, int stride,
  2257. int h_edge_pos, int v_edge_pos,
  2258. int w, int h, op_pixels_func *pix_op,
  2259. int motion_x, int motion_y)
  2260. {
  2261. int dxy;
  2262. int emu=0;
  2263. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  2264. src_x += motion_x >> 1;
  2265. src_y += motion_y >> 1;
  2266. /* WARNING: do no forget half pels */
  2267. src_x = clip(src_x, -16, width); //FIXME unneeded for emu?
  2268. if (src_x == width)
  2269. dxy &= ~1;
  2270. src_y = clip(src_y, -16, height);
  2271. if (src_y == height)
  2272. dxy &= ~2;
  2273. src += src_y * stride + src_x;
  2274. if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
  2275. if( (unsigned)src_x > h_edge_pos - (motion_x&1) - w
  2276. || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
  2277. ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  2278. src_x, src_y<<field_based, h_edge_pos, s->v_edge_pos);
  2279. src= s->edge_emu_buffer;
  2280. emu=1;
  2281. }
  2282. }
  2283. if(field_select)
  2284. src += s->linesize;
  2285. pix_op[dxy](dest, src, stride, h);
  2286. return emu;
  2287. }
  2288. static inline int hpel_motion_lowres(MpegEncContext *s,
  2289. uint8_t *dest, uint8_t *src,
  2290. int field_based, int field_select,
  2291. int src_x, int src_y,
  2292. int width, int height, int stride,
  2293. int h_edge_pos, int v_edge_pos,
  2294. int w, int h, h264_chroma_mc_func *pix_op,
  2295. int motion_x, int motion_y)
  2296. {
  2297. const int lowres= s->avctx->lowres;
  2298. const int s_mask= (2<<lowres)-1;
  2299. int emu=0;
  2300. int sx, sy;
  2301. if(s->quarter_sample){
  2302. motion_x/=2;
  2303. motion_y/=2;
  2304. }
  2305. sx= motion_x & s_mask;
  2306. sy= motion_y & s_mask;
  2307. src_x += motion_x >> (lowres+1);
  2308. src_y += motion_y >> (lowres+1);
  2309. src += src_y * stride + src_x;
  2310. if( (unsigned)src_x > h_edge_pos - (!!sx) - w
  2311. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  2312. ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  2313. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  2314. src= s->edge_emu_buffer;
  2315. emu=1;
  2316. }
  2317. sx <<= 2 - lowres;
  2318. sy <<= 2 - lowres;
  2319. if(field_select)
  2320. src += s->linesize;
  2321. pix_op[lowres](dest, src, stride, h, sx, sy);
  2322. return emu;
  2323. }
  2324. /* apply one mpeg motion vector to the three components */
  2325. static always_inline void mpeg_motion(MpegEncContext *s,
  2326. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2327. int field_based, int bottom_field, int field_select,
  2328. uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
  2329. int motion_x, int motion_y, int h)
  2330. {
  2331. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2332. int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize;
  2333. #if 0
  2334. if(s->quarter_sample)
  2335. {
  2336. motion_x>>=1;
  2337. motion_y>>=1;
  2338. }
  2339. #endif
  2340. v_edge_pos = s->v_edge_pos >> field_based;
  2341. linesize = s->current_picture.linesize[0] << field_based;
  2342. uvlinesize = s->current_picture.linesize[1] << field_based;
  2343. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  2344. src_x = s->mb_x* 16 + (motion_x >> 1);
  2345. src_y =(s->mb_y<<(4-field_based)) + (motion_y >> 1);
  2346. if (s->out_format == FMT_H263) {
  2347. if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
  2348. mx = (motion_x>>1)|(motion_x&1);
  2349. my = motion_y >>1;
  2350. uvdxy = ((my & 1) << 1) | (mx & 1);
  2351. uvsrc_x = s->mb_x* 8 + (mx >> 1);
  2352. uvsrc_y = (s->mb_y<<(3-field_based)) + (my >> 1);
  2353. }else{
  2354. uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
  2355. uvsrc_x = src_x>>1;
  2356. uvsrc_y = src_y>>1;
  2357. }
  2358. }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  2359. mx = motion_x / 4;
  2360. my = motion_y / 4;
  2361. uvdxy = 0;
  2362. uvsrc_x = s->mb_x*8 + mx;
  2363. uvsrc_y = s->mb_y*8 + my;
  2364. } else {
  2365. if(s->chroma_y_shift){
  2366. mx = motion_x / 2;
  2367. my = motion_y / 2;
  2368. uvdxy = ((my & 1) << 1) | (mx & 1);
  2369. uvsrc_x = s->mb_x* 8 + (mx >> 1);
  2370. uvsrc_y = (s->mb_y<<(3-field_based)) + (my >> 1);
  2371. } else {
  2372. if(s->chroma_x_shift){
  2373. //Chroma422
  2374. mx = motion_x / 2;
  2375. uvdxy = ((motion_y & 1) << 1) | (mx & 1);
  2376. uvsrc_x = s->mb_x* 8 + (mx >> 1);
  2377. uvsrc_y = src_y;
  2378. } else {
  2379. //Chroma444
  2380. uvdxy = dxy;
  2381. uvsrc_x = src_x;
  2382. uvsrc_y = src_y;
  2383. }
  2384. }
  2385. }
  2386. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  2387. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  2388. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  2389. if( (unsigned)src_x > s->h_edge_pos - (motion_x&1) - 16
  2390. || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
  2391. if(s->codec_id == CODEC_ID_MPEG2VIDEO ||
  2392. s->codec_id == CODEC_ID_MPEG1VIDEO){
  2393. av_log(s->avctx,AV_LOG_DEBUG,"MPEG motion vector out of boundary\n");
  2394. return ;
  2395. }
  2396. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  2397. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  2398. ptr_y = s->edge_emu_buffer;
  2399. if(!(s->flags&CODEC_FLAG_GRAY)){
  2400. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  2401. ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
  2402. uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2403. ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
  2404. uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2405. ptr_cb= uvbuf;
  2406. ptr_cr= uvbuf+16;
  2407. }
  2408. }
  2409. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
  2410. dest_y += s->linesize;
  2411. dest_cb+= s->uvlinesize;
  2412. dest_cr+= s->uvlinesize;
  2413. }
  2414. if(field_select){
  2415. ptr_y += s->linesize;
  2416. ptr_cb+= s->uvlinesize;
  2417. ptr_cr+= s->uvlinesize;
  2418. }
  2419. pix_op[0][dxy](dest_y, ptr_y, linesize, h);
  2420. if(!(s->flags&CODEC_FLAG_GRAY)){
  2421. pix_op[s->chroma_x_shift][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
  2422. pix_op[s->chroma_x_shift][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
  2423. }
  2424. if(s->out_format == FMT_H261){
  2425. ff_h261_loop_filter(s);
  2426. }
  2427. }
  2428. /* apply one mpeg motion vector to the three components */
  2429. static always_inline void mpeg_motion_lowres(MpegEncContext *s,
  2430. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2431. int field_based, int bottom_field, int field_select,
  2432. uint8_t **ref_picture, h264_chroma_mc_func *pix_op,
  2433. int motion_x, int motion_y, int h)
  2434. {
  2435. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2436. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy, uvsx, uvsy;
  2437. const int lowres= s->avctx->lowres;
  2438. const int block_s= 8>>lowres;
  2439. const int s_mask= (2<<lowres)-1;
  2440. const int h_edge_pos = s->h_edge_pos >> lowres;
  2441. const int v_edge_pos = s->v_edge_pos >> lowres;
  2442. linesize = s->current_picture.linesize[0] << field_based;
  2443. uvlinesize = s->current_picture.linesize[1] << field_based;
  2444. if(s->quarter_sample){ //FIXME obviously not perfect but qpel wont work in lowres anyway
  2445. motion_x/=2;
  2446. motion_y/=2;
  2447. }
  2448. if(field_based){
  2449. motion_y += (bottom_field - field_select)*((1<<lowres)-1);
  2450. }
  2451. sx= motion_x & s_mask;
  2452. sy= motion_y & s_mask;
  2453. src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1));
  2454. src_y =(s->mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1));
  2455. if (s->out_format == FMT_H263) {
  2456. uvsx = ((motion_x>>1) & s_mask) | (sx&1);
  2457. uvsy = ((motion_y>>1) & s_mask) | (sy&1);
  2458. uvsrc_x = src_x>>1;
  2459. uvsrc_y = src_y>>1;
  2460. }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  2461. mx = motion_x / 4;
  2462. my = motion_y / 4;
  2463. uvsx = (2*mx) & s_mask;
  2464. uvsy = (2*my) & s_mask;
  2465. uvsrc_x = s->mb_x*block_s + (mx >> lowres);
  2466. uvsrc_y = s->mb_y*block_s + (my >> lowres);
  2467. } else {
  2468. mx = motion_x / 2;
  2469. my = motion_y / 2;
  2470. uvsx = mx & s_mask;
  2471. uvsy = my & s_mask;
  2472. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  2473. uvsrc_y =(s->mb_y*block_s>>field_based) + (my >> (lowres+1));
  2474. }
  2475. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  2476. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  2477. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  2478. if( (unsigned)src_x > h_edge_pos - (!!sx) - 2*block_s
  2479. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  2480. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  2481. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  2482. ptr_y = s->edge_emu_buffer;
  2483. if(!(s->flags&CODEC_FLAG_GRAY)){
  2484. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  2485. ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
  2486. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  2487. ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
  2488. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  2489. ptr_cb= uvbuf;
  2490. ptr_cr= uvbuf+16;
  2491. }
  2492. }
  2493. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
  2494. dest_y += s->linesize;
  2495. dest_cb+= s->uvlinesize;
  2496. dest_cr+= s->uvlinesize;
  2497. }
  2498. if(field_select){
  2499. ptr_y += s->linesize;
  2500. ptr_cb+= s->uvlinesize;
  2501. ptr_cr+= s->uvlinesize;
  2502. }
  2503. sx <<= 2 - lowres;
  2504. sy <<= 2 - lowres;
  2505. pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy);
  2506. if(!(s->flags&CODEC_FLAG_GRAY)){
  2507. uvsx <<= 2 - lowres;
  2508. uvsy <<= 2 - lowres;
  2509. pix_op[lowres](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  2510. pix_op[lowres](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  2511. }
  2512. //FIXME h261 lowres loop filter
  2513. }
  2514. //FIXME move to dsputil, avg variant, 16x16 version
  2515. static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
  2516. int x;
  2517. uint8_t * const top = src[1];
  2518. uint8_t * const left = src[2];
  2519. uint8_t * const mid = src[0];
  2520. uint8_t * const right = src[3];
  2521. uint8_t * const bottom= src[4];
  2522. #define OBMC_FILTER(x, t, l, m, r, b)\
  2523. dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
  2524. #define OBMC_FILTER4(x, t, l, m, r, b)\
  2525. OBMC_FILTER(x , t, l, m, r, b);\
  2526. OBMC_FILTER(x+1 , t, l, m, r, b);\
  2527. OBMC_FILTER(x +stride, t, l, m, r, b);\
  2528. OBMC_FILTER(x+1+stride, t, l, m, r, b);
  2529. x=0;
  2530. OBMC_FILTER (x , 2, 2, 4, 0, 0);
  2531. OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
  2532. OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
  2533. OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
  2534. OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
  2535. OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
  2536. x+= stride;
  2537. OBMC_FILTER (x , 1, 2, 5, 0, 0);
  2538. OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
  2539. OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
  2540. OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
  2541. x+= stride;
  2542. OBMC_FILTER4(x , 1, 2, 5, 0, 0);
  2543. OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
  2544. OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
  2545. OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
  2546. x+= 2*stride;
  2547. OBMC_FILTER4(x , 0, 2, 5, 0, 1);
  2548. OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
  2549. OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
  2550. OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
  2551. x+= 2*stride;
  2552. OBMC_FILTER (x , 0, 2, 5, 0, 1);
  2553. OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
  2554. OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
  2555. OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
  2556. OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
  2557. OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
  2558. x+= stride;
  2559. OBMC_FILTER (x , 0, 2, 4, 0, 2);
  2560. OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
  2561. OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
  2562. OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
  2563. }
  2564. /* obmc for 1 8x8 luma block */
  2565. static inline void obmc_motion(MpegEncContext *s,
  2566. uint8_t *dest, uint8_t *src,
  2567. int src_x, int src_y,
  2568. op_pixels_func *pix_op,
  2569. int16_t mv[5][2]/* mid top left right bottom*/)
  2570. #define MID 0
  2571. {
  2572. int i;
  2573. uint8_t *ptr[5];
  2574. assert(s->quarter_sample==0);
  2575. for(i=0; i<5; i++){
  2576. if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
  2577. ptr[i]= ptr[MID];
  2578. }else{
  2579. ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
  2580. hpel_motion(s, ptr[i], src, 0, 0,
  2581. src_x, src_y,
  2582. s->width, s->height, s->linesize,
  2583. s->h_edge_pos, s->v_edge_pos,
  2584. 8, 8, pix_op,
  2585. mv[i][0], mv[i][1]);
  2586. }
  2587. }
  2588. put_obmc(dest, ptr, s->linesize);
  2589. }
  2590. static inline void qpel_motion(MpegEncContext *s,
  2591. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2592. int field_based, int bottom_field, int field_select,
  2593. uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
  2594. qpel_mc_func (*qpix_op)[16],
  2595. int motion_x, int motion_y, int h)
  2596. {
  2597. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2598. int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
  2599. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  2600. src_x = s->mb_x * 16 + (motion_x >> 2);
  2601. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  2602. v_edge_pos = s->v_edge_pos >> field_based;
  2603. linesize = s->linesize << field_based;
  2604. uvlinesize = s->uvlinesize << field_based;
  2605. if(field_based){
  2606. mx= motion_x/2;
  2607. my= motion_y>>1;
  2608. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
  2609. static const int rtab[8]= {0,0,1,1,0,0,0,1};
  2610. mx= (motion_x>>1) + rtab[motion_x&7];
  2611. my= (motion_y>>1) + rtab[motion_y&7];
  2612. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
  2613. mx= (motion_x>>1)|(motion_x&1);
  2614. my= (motion_y>>1)|(motion_y&1);
  2615. }else{
  2616. mx= motion_x/2;
  2617. my= motion_y/2;
  2618. }
  2619. mx= (mx>>1)|(mx&1);
  2620. my= (my>>1)|(my&1);
  2621. uvdxy= (mx&1) | ((my&1)<<1);
  2622. mx>>=1;
  2623. my>>=1;
  2624. uvsrc_x = s->mb_x * 8 + mx;
  2625. uvsrc_y = s->mb_y * (8 >> field_based) + my;
  2626. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  2627. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  2628. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  2629. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16
  2630. || (unsigned)src_y > v_edge_pos - (motion_y&3) - h ){
  2631. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  2632. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  2633. ptr_y= s->edge_emu_buffer;
  2634. if(!(s->flags&CODEC_FLAG_GRAY)){
  2635. uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
  2636. ff_emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize, 9, 9 + field_based,
  2637. uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2638. ff_emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize, 9, 9 + field_based,
  2639. uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2640. ptr_cb= uvbuf;
  2641. ptr_cr= uvbuf + 16;
  2642. }
  2643. }
  2644. if(!field_based)
  2645. qpix_op[0][dxy](dest_y, ptr_y, linesize);
  2646. else{
  2647. if(bottom_field){
  2648. dest_y += s->linesize;
  2649. dest_cb+= s->uvlinesize;
  2650. dest_cr+= s->uvlinesize;
  2651. }
  2652. if(field_select){
  2653. ptr_y += s->linesize;
  2654. ptr_cb += s->uvlinesize;
  2655. ptr_cr += s->uvlinesize;
  2656. }
  2657. //damn interlaced mode
  2658. //FIXME boundary mirroring is not exactly correct here
  2659. qpix_op[1][dxy](dest_y , ptr_y , linesize);
  2660. qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
  2661. }
  2662. if(!(s->flags&CODEC_FLAG_GRAY)){
  2663. pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
  2664. pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
  2665. }
  2666. }
  2667. inline int ff_h263_round_chroma(int x){
  2668. if (x >= 0)
  2669. return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  2670. else {
  2671. x = -x;
  2672. return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  2673. }
  2674. }
  2675. /**
  2676. * h263 chorma 4mv motion compensation.
  2677. */
  2678. static inline void chroma_4mv_motion(MpegEncContext *s,
  2679. uint8_t *dest_cb, uint8_t *dest_cr,
  2680. uint8_t **ref_picture,
  2681. op_pixels_func *pix_op,
  2682. int mx, int my){
  2683. int dxy, emu=0, src_x, src_y, offset;
  2684. uint8_t *ptr;
  2685. /* In case of 8X8, we construct a single chroma motion vector
  2686. with a special rounding */
  2687. mx= ff_h263_round_chroma(mx);
  2688. my= ff_h263_round_chroma(my);
  2689. dxy = ((my & 1) << 1) | (mx & 1);
  2690. mx >>= 1;
  2691. my >>= 1;
  2692. src_x = s->mb_x * 8 + mx;
  2693. src_y = s->mb_y * 8 + my;
  2694. src_x = clip(src_x, -8, s->width/2);
  2695. if (src_x == s->width/2)
  2696. dxy &= ~1;
  2697. src_y = clip(src_y, -8, s->height/2);
  2698. if (src_y == s->height/2)
  2699. dxy &= ~2;
  2700. offset = (src_y * (s->uvlinesize)) + src_x;
  2701. ptr = ref_picture[1] + offset;
  2702. if(s->flags&CODEC_FLAG_EMU_EDGE){
  2703. if( (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8
  2704. || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){
  2705. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2706. ptr= s->edge_emu_buffer;
  2707. emu=1;
  2708. }
  2709. }
  2710. pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
  2711. ptr = ref_picture[2] + offset;
  2712. if(emu){
  2713. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2714. ptr= s->edge_emu_buffer;
  2715. }
  2716. pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
  2717. }
  2718. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  2719. uint8_t *dest_cb, uint8_t *dest_cr,
  2720. uint8_t **ref_picture,
  2721. h264_chroma_mc_func *pix_op,
  2722. int mx, int my){
  2723. const int lowres= s->avctx->lowres;
  2724. const int block_s= 8>>lowres;
  2725. const int s_mask= (2<<lowres)-1;
  2726. const int h_edge_pos = s->h_edge_pos >> (lowres+1);
  2727. const int v_edge_pos = s->v_edge_pos >> (lowres+1);
  2728. int emu=0, src_x, src_y, offset, sx, sy;
  2729. uint8_t *ptr;
  2730. if(s->quarter_sample){
  2731. mx/=2;
  2732. my/=2;
  2733. }
  2734. /* In case of 8X8, we construct a single chroma motion vector
  2735. with a special rounding */
  2736. mx= ff_h263_round_chroma(mx);
  2737. my= ff_h263_round_chroma(my);
  2738. sx= mx & s_mask;
  2739. sy= my & s_mask;
  2740. src_x = s->mb_x*block_s + (mx >> (lowres+1));
  2741. src_y = s->mb_y*block_s + (my >> (lowres+1));
  2742. offset = src_y * s->uvlinesize + src_x;
  2743. ptr = ref_picture[1] + offset;
  2744. if(s->flags&CODEC_FLAG_EMU_EDGE){
  2745. if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s
  2746. || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){
  2747. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  2748. ptr= s->edge_emu_buffer;
  2749. emu=1;
  2750. }
  2751. }
  2752. sx <<= 2 - lowres;
  2753. sy <<= 2 - lowres;
  2754. pix_op[lowres](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  2755. ptr = ref_picture[2] + offset;
  2756. if(emu){
  2757. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  2758. ptr= s->edge_emu_buffer;
  2759. }
  2760. pix_op[lowres](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  2761. }
  2762. /**
  2763. * motion compesation of a single macroblock
  2764. * @param s context
  2765. * @param dest_y luma destination pointer
  2766. * @param dest_cb chroma cb/u destination pointer
  2767. * @param dest_cr chroma cr/v destination pointer
  2768. * @param dir direction (0->forward, 1->backward)
  2769. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  2770. * @param pic_op halfpel motion compensation function (average or put normally)
  2771. * @param pic_op qpel motion compensation function (average or put normally)
  2772. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2773. */
  2774. static inline void MPV_motion(MpegEncContext *s,
  2775. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2776. int dir, uint8_t **ref_picture,
  2777. op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
  2778. {
  2779. int dxy, mx, my, src_x, src_y, motion_x, motion_y;
  2780. int mb_x, mb_y, i;
  2781. uint8_t *ptr, *dest;
  2782. mb_x = s->mb_x;
  2783. mb_y = s->mb_y;
  2784. if(s->obmc && s->pict_type != B_TYPE){
  2785. int16_t mv_cache[4][4][2];
  2786. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  2787. const int mot_stride= s->b8_stride;
  2788. const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
  2789. assert(!s->mb_skiped);
  2790. memcpy(mv_cache[1][1], s->current_picture.motion_val[0][mot_xy ], sizeof(int16_t)*4);
  2791. memcpy(mv_cache[2][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
  2792. memcpy(mv_cache[3][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
  2793. if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){
  2794. memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
  2795. }else{
  2796. memcpy(mv_cache[0][1], s->current_picture.motion_val[0][mot_xy-mot_stride], sizeof(int16_t)*4);
  2797. }
  2798. if(mb_x==0 || IS_INTRA(s->current_picture.mb_type[xy-1])){
  2799. *(int32_t*)mv_cache[1][0]= *(int32_t*)mv_cache[1][1];
  2800. *(int32_t*)mv_cache[2][0]= *(int32_t*)mv_cache[2][1];
  2801. }else{
  2802. *(int32_t*)mv_cache[1][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1];
  2803. *(int32_t*)mv_cache[2][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1+mot_stride];
  2804. }
  2805. if(mb_x+1>=s->mb_width || IS_INTRA(s->current_picture.mb_type[xy+1])){
  2806. *(int32_t*)mv_cache[1][3]= *(int32_t*)mv_cache[1][2];
  2807. *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2];
  2808. }else{
  2809. *(int32_t*)mv_cache[1][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2];
  2810. *(int32_t*)mv_cache[2][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2+mot_stride];
  2811. }
  2812. mx = 0;
  2813. my = 0;
  2814. for(i=0;i<4;i++) {
  2815. const int x= (i&1)+1;
  2816. const int y= (i>>1)+1;
  2817. int16_t mv[5][2]= {
  2818. {mv_cache[y][x ][0], mv_cache[y][x ][1]},
  2819. {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
  2820. {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
  2821. {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
  2822. {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
  2823. //FIXME cleanup
  2824. obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  2825. ref_picture[0],
  2826. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  2827. pix_op[1],
  2828. mv);
  2829. mx += mv[0][0];
  2830. my += mv[0][1];
  2831. }
  2832. if(!(s->flags&CODEC_FLAG_GRAY))
  2833. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  2834. return;
  2835. }
  2836. switch(s->mv_type) {
  2837. case MV_TYPE_16X16:
  2838. #ifdef CONFIG_RISKY
  2839. if(s->mcsel){
  2840. if(s->real_sprite_warping_points==1){
  2841. gmc1_motion(s, dest_y, dest_cb, dest_cr,
  2842. ref_picture);
  2843. }else{
  2844. gmc_motion(s, dest_y, dest_cb, dest_cr,
  2845. ref_picture);
  2846. }
  2847. }else if(s->quarter_sample){
  2848. qpel_motion(s, dest_y, dest_cb, dest_cr,
  2849. 0, 0, 0,
  2850. ref_picture, pix_op, qpix_op,
  2851. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2852. }else if(s->mspel){
  2853. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  2854. ref_picture, pix_op,
  2855. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2856. }else
  2857. #endif
  2858. {
  2859. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  2860. 0, 0, 0,
  2861. ref_picture, pix_op,
  2862. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2863. }
  2864. break;
  2865. case MV_TYPE_8X8:
  2866. mx = 0;
  2867. my = 0;
  2868. if(s->quarter_sample){
  2869. for(i=0;i<4;i++) {
  2870. motion_x = s->mv[dir][i][0];
  2871. motion_y = s->mv[dir][i][1];
  2872. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  2873. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  2874. src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
  2875. /* WARNING: do no forget half pels */
  2876. src_x = clip(src_x, -16, s->width);
  2877. if (src_x == s->width)
  2878. dxy &= ~3;
  2879. src_y = clip(src_y, -16, s->height);
  2880. if (src_y == s->height)
  2881. dxy &= ~12;
  2882. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  2883. if(s->flags&CODEC_FLAG_EMU_EDGE){
  2884. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8
  2885. || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){
  2886. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  2887. ptr= s->edge_emu_buffer;
  2888. }
  2889. }
  2890. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  2891. qpix_op[1][dxy](dest, ptr, s->linesize);
  2892. mx += s->mv[dir][i][0]/2;
  2893. my += s->mv[dir][i][1]/2;
  2894. }
  2895. }else{
  2896. for(i=0;i<4;i++) {
  2897. hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  2898. ref_picture[0], 0, 0,
  2899. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  2900. s->width, s->height, s->linesize,
  2901. s->h_edge_pos, s->v_edge_pos,
  2902. 8, 8, pix_op[1],
  2903. s->mv[dir][i][0], s->mv[dir][i][1]);
  2904. mx += s->mv[dir][i][0];
  2905. my += s->mv[dir][i][1];
  2906. }
  2907. }
  2908. if(!(s->flags&CODEC_FLAG_GRAY))
  2909. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  2910. break;
  2911. case MV_TYPE_FIELD:
  2912. if (s->picture_structure == PICT_FRAME) {
  2913. if(s->quarter_sample){
  2914. for(i=0; i<2; i++){
  2915. qpel_motion(s, dest_y, dest_cb, dest_cr,
  2916. 1, i, s->field_select[dir][i],
  2917. ref_picture, pix_op, qpix_op,
  2918. s->mv[dir][i][0], s->mv[dir][i][1], 8);
  2919. }
  2920. }else{
  2921. /* top field */
  2922. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  2923. 1, 0, s->field_select[dir][0],
  2924. ref_picture, pix_op,
  2925. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2926. /* bottom field */
  2927. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  2928. 1, 1, s->field_select[dir][1],
  2929. ref_picture, pix_op,
  2930. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  2931. }
  2932. } else {
  2933. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){
  2934. ref_picture= s->current_picture_ptr->data;
  2935. }
  2936. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  2937. 0, 0, s->field_select[dir][0],
  2938. ref_picture, pix_op,
  2939. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2940. }
  2941. break;
  2942. case MV_TYPE_16X8:
  2943. for(i=0; i<2; i++){
  2944. uint8_t ** ref2picture;
  2945. if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){
  2946. ref2picture= ref_picture;
  2947. }else{
  2948. ref2picture= s->current_picture_ptr->data;
  2949. }
  2950. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  2951. 0, 0, s->field_select[dir][i],
  2952. ref2picture, pix_op,
  2953. s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8);
  2954. dest_y += 16*s->linesize;
  2955. dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
  2956. dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
  2957. }
  2958. break;
  2959. case MV_TYPE_DMV:
  2960. if(s->picture_structure == PICT_FRAME){
  2961. for(i=0; i<2; i++){
  2962. int j;
  2963. for(j=0; j<2; j++){
  2964. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  2965. 1, j, j^i,
  2966. ref_picture, pix_op,
  2967. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], 8);
  2968. }
  2969. pix_op = s->dsp.avg_pixels_tab;
  2970. }
  2971. }else{
  2972. for(i=0; i<2; i++){
  2973. mpeg_motion(s, dest_y, dest_cb, dest_cr,
  2974. 0, 0, s->picture_structure != i+1,
  2975. ref_picture, pix_op,
  2976. s->mv[dir][2*i][0],s->mv[dir][2*i][1],16);
  2977. // after put we make avg of the same block
  2978. pix_op=s->dsp.avg_pixels_tab;
  2979. //opposite parity is always in the same frame if this is second field
  2980. if(!s->first_field){
  2981. ref_picture = s->current_picture_ptr->data;
  2982. }
  2983. }
  2984. }
  2985. break;
  2986. default: assert(0);
  2987. }
  2988. }
  2989. /**
  2990. * motion compesation of a single macroblock
  2991. * @param s context
  2992. * @param dest_y luma destination pointer
  2993. * @param dest_cb chroma cb/u destination pointer
  2994. * @param dest_cr chroma cr/v destination pointer
  2995. * @param dir direction (0->forward, 1->backward)
  2996. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  2997. * @param pic_op halfpel motion compensation function (average or put normally)
  2998. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2999. */
  3000. static inline void MPV_motion_lowres(MpegEncContext *s,
  3001. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  3002. int dir, uint8_t **ref_picture,
  3003. h264_chroma_mc_func *pix_op)
  3004. {
  3005. int mx, my;
  3006. int mb_x, mb_y, i;
  3007. const int lowres= s->avctx->lowres;
  3008. const int block_s= 8>>lowres;
  3009. mb_x = s->mb_x;
  3010. mb_y = s->mb_y;
  3011. switch(s->mv_type) {
  3012. case MV_TYPE_16X16:
  3013. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  3014. 0, 0, 0,
  3015. ref_picture, pix_op,
  3016. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
  3017. break;
  3018. case MV_TYPE_8X8:
  3019. mx = 0;
  3020. my = 0;
  3021. for(i=0;i<4;i++) {
  3022. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) * s->linesize)*block_s,
  3023. ref_picture[0], 0, 0,
  3024. (2*mb_x + (i & 1))*block_s, (2*mb_y + (i >>1))*block_s,
  3025. s->width, s->height, s->linesize,
  3026. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  3027. block_s, block_s, pix_op,
  3028. s->mv[dir][i][0], s->mv[dir][i][1]);
  3029. mx += s->mv[dir][i][0];
  3030. my += s->mv[dir][i][1];
  3031. }
  3032. if(!(s->flags&CODEC_FLAG_GRAY))
  3033. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my);
  3034. break;
  3035. case MV_TYPE_FIELD:
  3036. if (s->picture_structure == PICT_FRAME) {
  3037. /* top field */
  3038. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  3039. 1, 0, s->field_select[dir][0],
  3040. ref_picture, pix_op,
  3041. s->mv[dir][0][0], s->mv[dir][0][1], block_s);
  3042. /* bottom field */
  3043. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  3044. 1, 1, s->field_select[dir][1],
  3045. ref_picture, pix_op,
  3046. s->mv[dir][1][0], s->mv[dir][1][1], block_s);
  3047. } else {
  3048. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){
  3049. ref_picture= s->current_picture_ptr->data;
  3050. }
  3051. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  3052. 0, 0, s->field_select[dir][0],
  3053. ref_picture, pix_op,
  3054. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
  3055. }
  3056. break;
  3057. case MV_TYPE_16X8:
  3058. for(i=0; i<2; i++){
  3059. uint8_t ** ref2picture;
  3060. if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){
  3061. ref2picture= ref_picture;
  3062. }else{
  3063. ref2picture= s->current_picture_ptr->data;
  3064. }
  3065. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  3066. 0, 0, s->field_select[dir][i],
  3067. ref2picture, pix_op,
  3068. s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s);
  3069. dest_y += 2*block_s*s->linesize;
  3070. dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  3071. dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  3072. }
  3073. break;
  3074. case MV_TYPE_DMV:
  3075. if(s->picture_structure == PICT_FRAME){
  3076. for(i=0; i<2; i++){
  3077. int j;
  3078. for(j=0; j<2; j++){
  3079. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  3080. 1, j, j^i,
  3081. ref_picture, pix_op,
  3082. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], block_s);
  3083. }
  3084. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  3085. }
  3086. }else{
  3087. for(i=0; i<2; i++){
  3088. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  3089. 0, 0, s->picture_structure != i+1,
  3090. ref_picture, pix_op,
  3091. s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s);
  3092. // after put we make avg of the same block
  3093. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  3094. //opposite parity is always in the same frame if this is second field
  3095. if(!s->first_field){
  3096. ref_picture = s->current_picture_ptr->data;
  3097. }
  3098. }
  3099. }
  3100. break;
  3101. default: assert(0);
  3102. }
  3103. }
  3104. /* put block[] to dest[] */
  3105. static inline void put_dct(MpegEncContext *s,
  3106. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  3107. {
  3108. s->dct_unquantize_intra(s, block, i, qscale);
  3109. s->dsp.idct_put (dest, line_size, block);
  3110. }
  3111. /* add block[] to dest[] */
  3112. static inline void add_dct(MpegEncContext *s,
  3113. DCTELEM *block, int i, uint8_t *dest, int line_size)
  3114. {
  3115. if (s->block_last_index[i] >= 0) {
  3116. s->dsp.idct_add (dest, line_size, block);
  3117. }
  3118. }
  3119. static inline void add_dequant_dct(MpegEncContext *s,
  3120. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  3121. {
  3122. if (s->block_last_index[i] >= 0) {
  3123. s->dct_unquantize_inter(s, block, i, qscale);
  3124. s->dsp.idct_add (dest, line_size, block);
  3125. }
  3126. }
  3127. /**
  3128. * cleans dc, ac, coded_block for the current non intra MB
  3129. */
  3130. void ff_clean_intra_table_entries(MpegEncContext *s)
  3131. {
  3132. int wrap = s->b8_stride;
  3133. int xy = s->block_index[0];
  3134. s->dc_val[0][xy ] =
  3135. s->dc_val[0][xy + 1 ] =
  3136. s->dc_val[0][xy + wrap] =
  3137. s->dc_val[0][xy + 1 + wrap] = 1024;
  3138. /* ac pred */
  3139. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  3140. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  3141. if (s->msmpeg4_version>=3) {
  3142. s->coded_block[xy ] =
  3143. s->coded_block[xy + 1 ] =
  3144. s->coded_block[xy + wrap] =
  3145. s->coded_block[xy + 1 + wrap] = 0;
  3146. }
  3147. /* chroma */
  3148. wrap = s->mb_stride;
  3149. xy = s->mb_x + s->mb_y * wrap;
  3150. s->dc_val[1][xy] =
  3151. s->dc_val[2][xy] = 1024;
  3152. /* ac pred */
  3153. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  3154. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  3155. s->mbintra_table[xy]= 0;
  3156. }
  3157. /* generic function called after a macroblock has been parsed by the
  3158. decoder or after it has been encoded by the encoder.
  3159. Important variables used:
  3160. s->mb_intra : true if intra macroblock
  3161. s->mv_dir : motion vector direction
  3162. s->mv_type : motion vector type
  3163. s->mv : motion vector
  3164. s->interlaced_dct : true if interlaced dct used (mpeg2)
  3165. */
  3166. static always_inline void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64], int lowres_flag)
  3167. {
  3168. int mb_x, mb_y;
  3169. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  3170. #ifdef HAVE_XVMC
  3171. if(s->avctx->xvmc_acceleration){
  3172. XVMC_decode_mb(s);//xvmc uses pblocks
  3173. return;
  3174. }
  3175. #endif
  3176. mb_x = s->mb_x;
  3177. mb_y = s->mb_y;
  3178. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  3179. /* save DCT coefficients */
  3180. int i,j;
  3181. DCTELEM *dct = &s->current_picture.dct_coeff[mb_xy*64*6];
  3182. for(i=0; i<6; i++)
  3183. for(j=0; j<64; j++)
  3184. *dct++ = block[i][s->dsp.idct_permutation[j]];
  3185. }
  3186. s->current_picture.qscale_table[mb_xy]= s->qscale;
  3187. /* update DC predictors for P macroblocks */
  3188. if (!s->mb_intra) {
  3189. if (s->h263_pred || s->h263_aic) {
  3190. if(s->mbintra_table[mb_xy])
  3191. ff_clean_intra_table_entries(s);
  3192. } else {
  3193. s->last_dc[0] =
  3194. s->last_dc[1] =
  3195. s->last_dc[2] = 128 << s->intra_dc_precision;
  3196. }
  3197. }
  3198. else if (s->h263_pred || s->h263_aic)
  3199. s->mbintra_table[mb_xy]=1;
  3200. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
  3201. uint8_t *dest_y, *dest_cb, *dest_cr;
  3202. int dct_linesize, dct_offset;
  3203. op_pixels_func (*op_pix)[4];
  3204. qpel_mc_func (*op_qpix)[16];
  3205. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  3206. const int uvlinesize= s->current_picture.linesize[1];
  3207. const int readable= s->pict_type != B_TYPE || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  3208. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  3209. /* avoid copy if macroblock skipped in last frame too */
  3210. /* skip only during decoding as we might trash the buffers during encoding a bit */
  3211. if(!s->encoding){
  3212. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  3213. const int age= s->current_picture.age;
  3214. assert(age);
  3215. if (s->mb_skiped) {
  3216. s->mb_skiped= 0;
  3217. assert(s->pict_type!=I_TYPE);
  3218. (*mbskip_ptr) ++; /* indicate that this time we skiped it */
  3219. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  3220. /* if previous was skipped too, then nothing to do ! */
  3221. if (*mbskip_ptr >= age && s->current_picture.reference){
  3222. return;
  3223. }
  3224. } else if(!s->current_picture.reference){
  3225. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  3226. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  3227. } else{
  3228. *mbskip_ptr = 0; /* not skipped */
  3229. }
  3230. }
  3231. dct_linesize = linesize << s->interlaced_dct;
  3232. dct_offset =(s->interlaced_dct)? linesize : linesize*block_size;
  3233. if(readable){
  3234. dest_y= s->dest[0];
  3235. dest_cb= s->dest[1];
  3236. dest_cr= s->dest[2];
  3237. }else{
  3238. dest_y = s->b_scratchpad;
  3239. dest_cb= s->b_scratchpad+16*linesize;
  3240. dest_cr= s->b_scratchpad+32*linesize;
  3241. }
  3242. if (!s->mb_intra) {
  3243. /* motion handling */
  3244. /* decoding or more than one mb_type (MC was allready done otherwise) */
  3245. if(!s->encoding){
  3246. if(lowres_flag){
  3247. h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
  3248. if (s->mv_dir & MV_DIR_FORWARD) {
  3249. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix);
  3250. op_pix = s->dsp.avg_h264_chroma_pixels_tab;
  3251. }
  3252. if (s->mv_dir & MV_DIR_BACKWARD) {
  3253. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix);
  3254. }
  3255. }else{
  3256. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  3257. op_pix = s->dsp.put_pixels_tab;
  3258. op_qpix= s->dsp.put_qpel_pixels_tab;
  3259. }else{
  3260. op_pix = s->dsp.put_no_rnd_pixels_tab;
  3261. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  3262. }
  3263. if (s->mv_dir & MV_DIR_FORWARD) {
  3264. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  3265. op_pix = s->dsp.avg_pixels_tab;
  3266. op_qpix= s->dsp.avg_qpel_pixels_tab;
  3267. }
  3268. if (s->mv_dir & MV_DIR_BACKWARD) {
  3269. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  3270. }
  3271. }
  3272. }
  3273. /* skip dequant / idct if we are really late ;) */
  3274. if(s->hurry_up>1) return;
  3275. /* add dct residue */
  3276. if(s->encoding || !( s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  3277. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  3278. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  3279. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  3280. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  3281. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  3282. if(!(s->flags&CODEC_FLAG_GRAY)){
  3283. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  3284. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  3285. }
  3286. } else if(s->codec_id != CODEC_ID_WMV2){
  3287. add_dct(s, block[0], 0, dest_y , dct_linesize);
  3288. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  3289. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  3290. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  3291. if(!(s->flags&CODEC_FLAG_GRAY)){
  3292. if(s->chroma_y_shift){//Chroma420
  3293. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  3294. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  3295. }else{
  3296. //chroma422
  3297. dct_linesize = uvlinesize << s->interlaced_dct;
  3298. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  3299. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  3300. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  3301. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  3302. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  3303. if(!s->chroma_x_shift){//Chroma444
  3304. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  3305. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  3306. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  3307. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  3308. }
  3309. }
  3310. }//fi gray
  3311. }
  3312. #ifdef CONFIG_RISKY
  3313. else{
  3314. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  3315. }
  3316. #endif
  3317. } else {
  3318. /* dct only in intra block */
  3319. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  3320. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  3321. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  3322. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  3323. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  3324. if(!(s->flags&CODEC_FLAG_GRAY)){
  3325. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  3326. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  3327. }
  3328. }else{
  3329. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  3330. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  3331. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  3332. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  3333. if(!(s->flags&CODEC_FLAG_GRAY)){
  3334. if(s->chroma_y_shift){
  3335. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  3336. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  3337. }else{
  3338. dct_linesize = uvlinesize << s->interlaced_dct;
  3339. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  3340. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  3341. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  3342. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  3343. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  3344. if(!s->chroma_x_shift){//Chroma444
  3345. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  3346. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  3347. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  3348. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  3349. }
  3350. }
  3351. }//gray
  3352. }
  3353. }
  3354. if(!readable){
  3355. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  3356. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  3357. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  3358. }
  3359. }
  3360. }
  3361. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
  3362. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1);
  3363. else MPV_decode_mb_internal(s, block, 0);
  3364. }
  3365. #ifdef CONFIG_ENCODERS
  3366. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
  3367. {
  3368. static const char tab[64]=
  3369. {3,2,2,1,1,1,1,1,
  3370. 1,1,1,1,1,1,1,1,
  3371. 1,1,1,1,1,1,1,1,
  3372. 0,0,0,0,0,0,0,0,
  3373. 0,0,0,0,0,0,0,0,
  3374. 0,0,0,0,0,0,0,0,
  3375. 0,0,0,0,0,0,0,0,
  3376. 0,0,0,0,0,0,0,0};
  3377. int score=0;
  3378. int run=0;
  3379. int i;
  3380. DCTELEM *block= s->block[n];
  3381. const int last_index= s->block_last_index[n];
  3382. int skip_dc;
  3383. if(threshold<0){
  3384. skip_dc=0;
  3385. threshold= -threshold;
  3386. }else
  3387. skip_dc=1;
  3388. /* are all which we could set to zero are allready zero? */
  3389. if(last_index<=skip_dc - 1) return;
  3390. for(i=0; i<=last_index; i++){
  3391. const int j = s->intra_scantable.permutated[i];
  3392. const int level = ABS(block[j]);
  3393. if(level==1){
  3394. if(skip_dc && i==0) continue;
  3395. score+= tab[run];
  3396. run=0;
  3397. }else if(level>1){
  3398. return;
  3399. }else{
  3400. run++;
  3401. }
  3402. }
  3403. if(score >= threshold) return;
  3404. for(i=skip_dc; i<=last_index; i++){
  3405. const int j = s->intra_scantable.permutated[i];
  3406. block[j]=0;
  3407. }
  3408. if(block[0]) s->block_last_index[n]= 0;
  3409. else s->block_last_index[n]= -1;
  3410. }
  3411. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  3412. {
  3413. int i;
  3414. const int maxlevel= s->max_qcoeff;
  3415. const int minlevel= s->min_qcoeff;
  3416. int overflow=0;
  3417. if(s->mb_intra){
  3418. i=1; //skip clipping of intra dc
  3419. }else
  3420. i=0;
  3421. for(;i<=last_index; i++){
  3422. const int j= s->intra_scantable.permutated[i];
  3423. int level = block[j];
  3424. if (level>maxlevel){
  3425. level=maxlevel;
  3426. overflow++;
  3427. }else if(level<minlevel){
  3428. level=minlevel;
  3429. overflow++;
  3430. }
  3431. block[j]= level;
  3432. }
  3433. if(overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
  3434. av_log(s->avctx, AV_LOG_INFO, "warning, cliping %d dct coefficents to %d..%d\n", overflow, minlevel, maxlevel);
  3435. }
  3436. #endif //CONFIG_ENCODERS
  3437. /**
  3438. *
  3439. * @param h is the normal height, this will be reduced automatically if needed for the last row
  3440. */
  3441. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  3442. if (s->avctx->draw_horiz_band) {
  3443. AVFrame *src;
  3444. int offset[4];
  3445. if(s->picture_structure != PICT_FRAME){
  3446. h <<= 1;
  3447. y <<= 1;
  3448. if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  3449. }
  3450. h= FFMIN(h, s->avctx->height - y);
  3451. if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  3452. src= (AVFrame*)s->current_picture_ptr;
  3453. else if(s->last_picture_ptr)
  3454. src= (AVFrame*)s->last_picture_ptr;
  3455. else
  3456. return;
  3457. if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  3458. offset[0]=
  3459. offset[1]=
  3460. offset[2]=
  3461. offset[3]= 0;
  3462. }else{
  3463. offset[0]= y * s->linesize;;
  3464. offset[1]=
  3465. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  3466. offset[3]= 0;
  3467. }
  3468. emms_c();
  3469. s->avctx->draw_horiz_band(s->avctx, src, offset,
  3470. y, s->picture_structure, h);
  3471. }
  3472. }
  3473. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  3474. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  3475. const int uvlinesize= s->current_picture.linesize[1];
  3476. const int mb_size= 4 - s->avctx->lowres;
  3477. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  3478. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  3479. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  3480. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  3481. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  3482. 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;
  3483. //block_index is not used by mpeg2, so it is not affected by chroma_format
  3484. s->dest[0] = s->current_picture.data[0] + ((s->mb_x - 1) << mb_size);
  3485. s->dest[1] = s->current_picture.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  3486. s->dest[2] = s->current_picture.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  3487. if(!(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  3488. {
  3489. s->dest[0] += s->mb_y * linesize << mb_size;
  3490. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  3491. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  3492. }
  3493. }
  3494. #ifdef CONFIG_ENCODERS
  3495. static void get_vissual_weight(int16_t *weight, uint8_t *ptr, int stride){
  3496. int x, y;
  3497. //FIXME optimize
  3498. for(y=0; y<8; y++){
  3499. for(x=0; x<8; x++){
  3500. int x2, y2;
  3501. int sum=0;
  3502. int sqr=0;
  3503. int count=0;
  3504. for(y2= FFMAX(y-1, 0); y2 < FFMIN(8, y+2); y2++){
  3505. for(x2= FFMAX(x-1, 0); x2 < FFMIN(8, x+2); x2++){
  3506. int v= ptr[x2 + y2*stride];
  3507. sum += v;
  3508. sqr += v*v;
  3509. count++;
  3510. }
  3511. }
  3512. weight[x + 8*y]= (36*ff_sqrt(count*sqr - sum*sum)) / count;
  3513. }
  3514. }
  3515. }
  3516. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  3517. {
  3518. int16_t weight[6][64];
  3519. DCTELEM orig[6][64];
  3520. const int mb_x= s->mb_x;
  3521. const int mb_y= s->mb_y;
  3522. int i;
  3523. int skip_dct[6];
  3524. int dct_offset = s->linesize*8; //default for progressive frames
  3525. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  3526. int wrap_y, wrap_c;
  3527. for(i=0; i<6; i++) skip_dct[i]=0;
  3528. if(s->adaptive_quant){
  3529. const int last_qp= s->qscale;
  3530. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3531. s->lambda= s->lambda_table[mb_xy];
  3532. update_qscale(s);
  3533. if(!(s->flags&CODEC_FLAG_QP_RD)){
  3534. s->dquant= s->qscale - last_qp;
  3535. if(s->out_format==FMT_H263){
  3536. s->dquant= clip(s->dquant, -2, 2); //FIXME RD
  3537. if(s->codec_id==CODEC_ID_MPEG4){
  3538. if(!s->mb_intra){
  3539. if(s->pict_type == B_TYPE){
  3540. if(s->dquant&1)
  3541. s->dquant= (s->dquant/2)*2;
  3542. if(s->mv_dir&MV_DIRECT)
  3543. s->dquant= 0;
  3544. }
  3545. if(s->mv_type==MV_TYPE_8X8)
  3546. s->dquant=0;
  3547. }
  3548. }
  3549. }
  3550. }
  3551. ff_set_qscale(s, last_qp + s->dquant);
  3552. }else if(s->flags&CODEC_FLAG_QP_RD)
  3553. ff_set_qscale(s, s->qscale + s->dquant);
  3554. wrap_y = s->linesize;
  3555. wrap_c = s->uvlinesize;
  3556. ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  3557. ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  3558. ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  3559. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  3560. uint8_t *ebuf= s->edge_emu_buffer + 32;
  3561. ff_emulated_edge_mc(ebuf , ptr_y , wrap_y,16,16,mb_x*16,mb_y*16, s->width , s->height);
  3562. ptr_y= ebuf;
  3563. ff_emulated_edge_mc(ebuf+18*wrap_y , ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  3564. ptr_cb= ebuf+18*wrap_y;
  3565. ff_emulated_edge_mc(ebuf+18*wrap_y+8, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  3566. ptr_cr= ebuf+18*wrap_y+8;
  3567. }
  3568. if (s->mb_intra) {
  3569. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  3570. int progressive_score, interlaced_score;
  3571. s->interlaced_dct=0;
  3572. progressive_score= s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y, 8)
  3573. +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y*8, NULL, wrap_y, 8) - 400;
  3574. if(progressive_score > 0){
  3575. interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y*2, 8)
  3576. +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y , NULL, wrap_y*2, 8);
  3577. if(progressive_score > interlaced_score){
  3578. s->interlaced_dct=1;
  3579. dct_offset= wrap_y;
  3580. wrap_y<<=1;
  3581. }
  3582. }
  3583. }
  3584. s->dsp.get_pixels(s->block[0], ptr_y , wrap_y);
  3585. s->dsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
  3586. s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y);
  3587. s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
  3588. if(s->flags&CODEC_FLAG_GRAY){
  3589. skip_dct[4]= 1;
  3590. skip_dct[5]= 1;
  3591. }else{
  3592. s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
  3593. s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
  3594. }
  3595. }else{
  3596. op_pixels_func (*op_pix)[4];
  3597. qpel_mc_func (*op_qpix)[16];
  3598. uint8_t *dest_y, *dest_cb, *dest_cr;
  3599. dest_y = s->dest[0];
  3600. dest_cb = s->dest[1];
  3601. dest_cr = s->dest[2];
  3602. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  3603. op_pix = s->dsp.put_pixels_tab;
  3604. op_qpix= s->dsp.put_qpel_pixels_tab;
  3605. }else{
  3606. op_pix = s->dsp.put_no_rnd_pixels_tab;
  3607. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  3608. }
  3609. if (s->mv_dir & MV_DIR_FORWARD) {
  3610. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  3611. op_pix = s->dsp.avg_pixels_tab;
  3612. op_qpix= s->dsp.avg_qpel_pixels_tab;
  3613. }
  3614. if (s->mv_dir & MV_DIR_BACKWARD) {
  3615. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  3616. }
  3617. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  3618. int progressive_score, interlaced_score;
  3619. s->interlaced_dct=0;
  3620. progressive_score= s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y, 8)
  3621. +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400;
  3622. if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400;
  3623. if(progressive_score>0){
  3624. interlaced_score = s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y*2, 8)
  3625. +s->dsp.ildct_cmp[0](s, dest_y + wrap_y , ptr_y + wrap_y , wrap_y*2, 8);
  3626. if(progressive_score > interlaced_score){
  3627. s->interlaced_dct=1;
  3628. dct_offset= wrap_y;
  3629. wrap_y<<=1;
  3630. }
  3631. }
  3632. }
  3633. s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  3634. s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  3635. s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
  3636. s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
  3637. if(s->flags&CODEC_FLAG_GRAY){
  3638. skip_dct[4]= 1;
  3639. skip_dct[5]= 1;
  3640. }else{
  3641. s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  3642. s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  3643. }
  3644. /* pre quantization */
  3645. if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
  3646. //FIXME optimize
  3647. if(s->dsp.sad[1](NULL, ptr_y , dest_y , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1;
  3648. if(s->dsp.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1;
  3649. if(s->dsp.sad[1](NULL, ptr_y +dct_offset , dest_y +dct_offset , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1;
  3650. if(s->dsp.sad[1](NULL, ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y, 8) < 20*s->qscale) skip_dct[3]= 1;
  3651. if(s->dsp.sad[1](NULL, ptr_cb , dest_cb , wrap_c, 8) < 20*s->qscale) skip_dct[4]= 1;
  3652. if(s->dsp.sad[1](NULL, ptr_cr , dest_cr , wrap_c, 8) < 20*s->qscale) skip_dct[5]= 1;
  3653. }
  3654. }
  3655. if(s->avctx->quantizer_noise_shaping){
  3656. if(!skip_dct[0]) get_vissual_weight(weight[0], ptr_y , wrap_y);
  3657. if(!skip_dct[1]) get_vissual_weight(weight[1], ptr_y + 8, wrap_y);
  3658. if(!skip_dct[2]) get_vissual_weight(weight[2], ptr_y + dct_offset , wrap_y);
  3659. if(!skip_dct[3]) get_vissual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
  3660. if(!skip_dct[4]) get_vissual_weight(weight[4], ptr_cb , wrap_c);
  3661. if(!skip_dct[5]) get_vissual_weight(weight[5], ptr_cr , wrap_c);
  3662. memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*6);
  3663. }
  3664. /* DCT & quantize */
  3665. assert(s->out_format!=FMT_MJPEG || s->qscale==8);
  3666. {
  3667. for(i=0;i<6;i++) {
  3668. if(!skip_dct[i]){
  3669. int overflow;
  3670. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  3671. // FIXME we could decide to change to quantizer instead of clipping
  3672. // JS: I don't think that would be a good idea it could lower quality instead
  3673. // of improve it. Just INTRADC clipping deserves changes in quantizer
  3674. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  3675. }else
  3676. s->block_last_index[i]= -1;
  3677. }
  3678. if(s->avctx->quantizer_noise_shaping){
  3679. for(i=0;i<6;i++) {
  3680. if(!skip_dct[i]){
  3681. s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale);
  3682. }
  3683. }
  3684. }
  3685. if(s->luma_elim_threshold && !s->mb_intra)
  3686. for(i=0; i<4; i++)
  3687. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  3688. if(s->chroma_elim_threshold && !s->mb_intra)
  3689. for(i=4; i<6; i++)
  3690. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  3691. if(s->flags & CODEC_FLAG_CBP_RD){
  3692. for(i=0;i<6;i++) {
  3693. if(s->block_last_index[i] == -1)
  3694. s->coded_score[i]= INT_MAX/256;
  3695. }
  3696. }
  3697. }
  3698. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  3699. s->block_last_index[4]=
  3700. s->block_last_index[5]= 0;
  3701. s->block[4][0]=
  3702. s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
  3703. }
  3704. //non c quantize code returns incorrect block_last_index FIXME
  3705. if(s->alternate_scan && s->dct_quantize != dct_quantize_c){
  3706. for(i=0; i<6; i++){
  3707. int j;
  3708. if(s->block_last_index[i]>0){
  3709. for(j=63; j>0; j--){
  3710. if(s->block[i][ s->intra_scantable.permutated[j] ]) break;
  3711. }
  3712. s->block_last_index[i]= j;
  3713. }
  3714. }
  3715. }
  3716. /* huffman encode */
  3717. switch(s->codec_id){ //FIXME funct ptr could be slightly faster
  3718. case CODEC_ID_MPEG1VIDEO:
  3719. case CODEC_ID_MPEG2VIDEO:
  3720. mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
  3721. #ifdef CONFIG_RISKY
  3722. case CODEC_ID_MPEG4:
  3723. mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  3724. case CODEC_ID_MSMPEG4V2:
  3725. case CODEC_ID_MSMPEG4V3:
  3726. case CODEC_ID_WMV1:
  3727. msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  3728. case CODEC_ID_WMV2:
  3729. ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
  3730. case CODEC_ID_H261:
  3731. ff_h261_encode_mb(s, s->block, motion_x, motion_y); break;
  3732. case CODEC_ID_H263:
  3733. case CODEC_ID_H263P:
  3734. case CODEC_ID_FLV1:
  3735. case CODEC_ID_RV10:
  3736. case CODEC_ID_RV20:
  3737. h263_encode_mb(s, s->block, motion_x, motion_y); break;
  3738. #endif
  3739. case CODEC_ID_MJPEG:
  3740. mjpeg_encode_mb(s, s->block); break;
  3741. default:
  3742. assert(0);
  3743. }
  3744. }
  3745. #endif //CONFIG_ENCODERS
  3746. void ff_mpeg_flush(AVCodecContext *avctx){
  3747. int i;
  3748. MpegEncContext *s = avctx->priv_data;
  3749. if(s==NULL || s->picture==NULL)
  3750. return;
  3751. for(i=0; i<MAX_PICTURE_COUNT; i++){
  3752. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  3753. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  3754. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  3755. }
  3756. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  3757. s->mb_x= s->mb_y= 0;
  3758. s->parse_context.state= -1;
  3759. s->parse_context.frame_start_found= 0;
  3760. s->parse_context.overread= 0;
  3761. s->parse_context.overread_index= 0;
  3762. s->parse_context.index= 0;
  3763. s->parse_context.last_index= 0;
  3764. s->bitstream_buffer_size=0;
  3765. }
  3766. #ifdef CONFIG_ENCODERS
  3767. void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
  3768. {
  3769. const uint16_t *srcw= (uint16_t*)src;
  3770. int words= length>>4;
  3771. int bits= length&15;
  3772. int i;
  3773. if(length==0) return;
  3774. if(words < 16){
  3775. for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
  3776. }else if(put_bits_count(pb)&7){
  3777. for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
  3778. }else{
  3779. for(i=0; put_bits_count(pb)&31; i++)
  3780. put_bits(pb, 8, src[i]);
  3781. flush_put_bits(pb);
  3782. memcpy(pbBufPtr(pb), src+i, 2*words-i);
  3783. skip_put_bytes(pb, 2*words-i);
  3784. }
  3785. put_bits(pb, bits, be2me_16(srcw[words])>>(16-bits));
  3786. }
  3787. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  3788. int i;
  3789. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  3790. /* mpeg1 */
  3791. d->mb_skip_run= s->mb_skip_run;
  3792. for(i=0; i<3; i++)
  3793. d->last_dc[i]= s->last_dc[i];
  3794. /* statistics */
  3795. d->mv_bits= s->mv_bits;
  3796. d->i_tex_bits= s->i_tex_bits;
  3797. d->p_tex_bits= s->p_tex_bits;
  3798. d->i_count= s->i_count;
  3799. d->f_count= s->f_count;
  3800. d->b_count= s->b_count;
  3801. d->skip_count= s->skip_count;
  3802. d->misc_bits= s->misc_bits;
  3803. d->last_bits= 0;
  3804. d->mb_skiped= 0;
  3805. d->qscale= s->qscale;
  3806. d->dquant= s->dquant;
  3807. }
  3808. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  3809. int i;
  3810. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  3811. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  3812. /* mpeg1 */
  3813. d->mb_skip_run= s->mb_skip_run;
  3814. for(i=0; i<3; i++)
  3815. d->last_dc[i]= s->last_dc[i];
  3816. /* statistics */
  3817. d->mv_bits= s->mv_bits;
  3818. d->i_tex_bits= s->i_tex_bits;
  3819. d->p_tex_bits= s->p_tex_bits;
  3820. d->i_count= s->i_count;
  3821. d->f_count= s->f_count;
  3822. d->b_count= s->b_count;
  3823. d->skip_count= s->skip_count;
  3824. d->misc_bits= s->misc_bits;
  3825. d->mb_intra= s->mb_intra;
  3826. d->mb_skiped= s->mb_skiped;
  3827. d->mv_type= s->mv_type;
  3828. d->mv_dir= s->mv_dir;
  3829. d->pb= s->pb;
  3830. if(s->data_partitioning){
  3831. d->pb2= s->pb2;
  3832. d->tex_pb= s->tex_pb;
  3833. }
  3834. d->block= s->block;
  3835. for(i=0; i<6; i++)
  3836. d->block_last_index[i]= s->block_last_index[i];
  3837. d->interlaced_dct= s->interlaced_dct;
  3838. d->qscale= s->qscale;
  3839. }
  3840. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  3841. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  3842. int *dmin, int *next_block, int motion_x, int motion_y)
  3843. {
  3844. int score;
  3845. uint8_t *dest_backup[3];
  3846. copy_context_before_encode(s, backup, type);
  3847. s->block= s->blocks[*next_block];
  3848. s->pb= pb[*next_block];
  3849. if(s->data_partitioning){
  3850. s->pb2 = pb2 [*next_block];
  3851. s->tex_pb= tex_pb[*next_block];
  3852. }
  3853. if(*next_block){
  3854. memcpy(dest_backup, s->dest, sizeof(s->dest));
  3855. s->dest[0] = s->rd_scratchpad;
  3856. s->dest[1] = s->rd_scratchpad + 16*s->linesize;
  3857. s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
  3858. assert(s->linesize >= 32); //FIXME
  3859. }
  3860. encode_mb(s, motion_x, motion_y);
  3861. score= put_bits_count(&s->pb);
  3862. if(s->data_partitioning){
  3863. score+= put_bits_count(&s->pb2);
  3864. score+= put_bits_count(&s->tex_pb);
  3865. }
  3866. if(s->avctx->mb_decision == FF_MB_DECISION_RD){
  3867. MPV_decode_mb(s, s->block);
  3868. score *= s->lambda2;
  3869. score += sse_mb(s) << FF_LAMBDA_SHIFT;
  3870. }
  3871. if(*next_block){
  3872. memcpy(s->dest, dest_backup, sizeof(s->dest));
  3873. }
  3874. if(score<*dmin){
  3875. *dmin= score;
  3876. *next_block^=1;
  3877. copy_context_after_encode(best, s, type);
  3878. }
  3879. }
  3880. static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
  3881. uint32_t *sq = squareTbl + 256;
  3882. int acc=0;
  3883. int x,y;
  3884. if(w==16 && h==16)
  3885. return s->dsp.sse[0](NULL, src1, src2, stride, 16);
  3886. else if(w==8 && h==8)
  3887. return s->dsp.sse[1](NULL, src1, src2, stride, 8);
  3888. for(y=0; y<h; y++){
  3889. for(x=0; x<w; x++){
  3890. acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  3891. }
  3892. }
  3893. assert(acc>=0);
  3894. return acc;
  3895. }
  3896. static int sse_mb(MpegEncContext *s){
  3897. int w= 16;
  3898. int h= 16;
  3899. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  3900. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  3901. if(w==16 && h==16)
  3902. if(s->avctx->mb_cmp == FF_CMP_NSSE){
  3903. return s->dsp.nsse[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
  3904. +s->dsp.nsse[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
  3905. +s->dsp.nsse[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
  3906. }else{
  3907. return s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
  3908. +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
  3909. +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
  3910. }
  3911. else
  3912. return sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
  3913. +sse(s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
  3914. +sse(s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
  3915. }
  3916. static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
  3917. MpegEncContext *s= arg;
  3918. s->me.pre_pass=1;
  3919. s->me.dia_size= s->avctx->pre_dia_size;
  3920. s->first_slice_line=1;
  3921. for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
  3922. for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
  3923. ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
  3924. }
  3925. s->first_slice_line=0;
  3926. }
  3927. s->me.pre_pass=0;
  3928. return 0;
  3929. }
  3930. static int estimate_motion_thread(AVCodecContext *c, void *arg){
  3931. MpegEncContext *s= arg;
  3932. s->me.dia_size= s->avctx->dia_size;
  3933. s->first_slice_line=1;
  3934. for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
  3935. s->mb_x=0; //for block init below
  3936. ff_init_block_index(s);
  3937. for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  3938. s->block_index[0]+=2;
  3939. s->block_index[1]+=2;
  3940. s->block_index[2]+=2;
  3941. s->block_index[3]+=2;
  3942. /* compute motion vector & mb_type and store in context */
  3943. if(s->pict_type==B_TYPE)
  3944. ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
  3945. else
  3946. ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
  3947. }
  3948. s->first_slice_line=0;
  3949. }
  3950. return 0;
  3951. }
  3952. static int mb_var_thread(AVCodecContext *c, void *arg){
  3953. MpegEncContext *s= arg;
  3954. int mb_x, mb_y;
  3955. for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
  3956. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  3957. int xx = mb_x * 16;
  3958. int yy = mb_y * 16;
  3959. uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  3960. int varc;
  3961. int sum = s->dsp.pix_sum(pix, s->linesize);
  3962. varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  3963. s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
  3964. s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  3965. s->me.mb_var_sum_temp += varc;
  3966. }
  3967. }
  3968. return 0;
  3969. }
  3970. static void write_slice_end(MpegEncContext *s){
  3971. if(s->codec_id==CODEC_ID_MPEG4){
  3972. if(s->partitioned_frame){
  3973. ff_mpeg4_merge_partitions(s);
  3974. }
  3975. ff_mpeg4_stuffing(&s->pb);
  3976. }else if(s->out_format == FMT_MJPEG){
  3977. ff_mjpeg_stuffing(&s->pb);
  3978. }
  3979. align_put_bits(&s->pb);
  3980. flush_put_bits(&s->pb);
  3981. if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
  3982. s->misc_bits+= get_bits_diff(s);
  3983. }
  3984. static int encode_thread(AVCodecContext *c, void *arg){
  3985. MpegEncContext *s= arg;
  3986. int mb_x, mb_y, pdif = 0;
  3987. int i, j;
  3988. MpegEncContext best_s, backup_s;
  3989. uint8_t bit_buf[2][MAX_MB_BYTES];
  3990. uint8_t bit_buf2[2][MAX_MB_BYTES];
  3991. uint8_t bit_buf_tex[2][MAX_MB_BYTES];
  3992. PutBitContext pb[2], pb2[2], tex_pb[2];
  3993. //printf("%d->%d\n", s->resync_mb_y, s->end_mb_y);
  3994. for(i=0; i<2; i++){
  3995. init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
  3996. init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
  3997. init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
  3998. }
  3999. s->last_bits= put_bits_count(&s->pb);
  4000. s->mv_bits=0;
  4001. s->misc_bits=0;
  4002. s->i_tex_bits=0;
  4003. s->p_tex_bits=0;
  4004. s->i_count=0;
  4005. s->f_count=0;
  4006. s->b_count=0;
  4007. s->skip_count=0;
  4008. for(i=0; i<3; i++){
  4009. /* init last dc values */
  4010. /* note: quant matrix value (8) is implied here */
  4011. s->last_dc[i] = 128 << s->intra_dc_precision;
  4012. s->current_picture_ptr->error[i] = 0;
  4013. }
  4014. s->mb_skip_run = 0;
  4015. memset(s->last_mv, 0, sizeof(s->last_mv));
  4016. s->last_mv_dir = 0;
  4017. #ifdef CONFIG_RISKY
  4018. switch(s->codec_id){
  4019. case CODEC_ID_H263:
  4020. case CODEC_ID_H263P:
  4021. case CODEC_ID_FLV1:
  4022. s->gob_index = ff_h263_get_gob_height(s);
  4023. break;
  4024. case CODEC_ID_MPEG4:
  4025. if(s->partitioned_frame)
  4026. ff_mpeg4_init_partitions(s);
  4027. break;
  4028. }
  4029. #endif
  4030. s->resync_mb_x=0;
  4031. s->resync_mb_y=0;
  4032. s->first_slice_line = 1;
  4033. s->ptr_lastgob = s->pb.buf;
  4034. for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
  4035. // printf("row %d at %X\n", s->mb_y, (int)s);
  4036. s->mb_x=0;
  4037. s->mb_y= mb_y;
  4038. ff_set_qscale(s, s->qscale);
  4039. ff_init_block_index(s);
  4040. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  4041. int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
  4042. int mb_type= s->mb_type[xy];
  4043. // int d;
  4044. int dmin= INT_MAX;
  4045. int dir;
  4046. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
  4047. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  4048. return -1;
  4049. }
  4050. if(s->data_partitioning){
  4051. if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
  4052. || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
  4053. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  4054. return -1;
  4055. }
  4056. }
  4057. s->mb_x = mb_x;
  4058. s->mb_y = mb_y; // moved into loop, can get changed by H.261
  4059. ff_update_block_index(s);
  4060. if(s->codec_id == CODEC_ID_H261){
  4061. ff_h261_reorder_mb_index(s);
  4062. xy= s->mb_y*s->mb_stride + s->mb_x;
  4063. mb_type= s->mb_type[xy];
  4064. }
  4065. /* write gob / video packet header */
  4066. #ifdef CONFIG_RISKY
  4067. if(s->rtp_mode){
  4068. int current_packet_size, is_gob_start;
  4069. current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
  4070. is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
  4071. if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
  4072. switch(s->codec_id){
  4073. case CODEC_ID_H263:
  4074. case CODEC_ID_H263P:
  4075. if(!s->h263_slice_structured)
  4076. if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
  4077. break;
  4078. case CODEC_ID_MPEG2VIDEO:
  4079. if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
  4080. case CODEC_ID_MPEG1VIDEO:
  4081. if(s->mb_skip_run) is_gob_start=0;
  4082. break;
  4083. }
  4084. if(is_gob_start){
  4085. if(s->start_mb_y != mb_y || mb_x!=0){
  4086. write_slice_end(s);
  4087. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
  4088. ff_mpeg4_init_partitions(s);
  4089. }
  4090. }
  4091. assert((put_bits_count(&s->pb)&7) == 0);
  4092. current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
  4093. if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
  4094. int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
  4095. int d= 100 / s->avctx->error_rate;
  4096. if(r % d == 0){
  4097. current_packet_size=0;
  4098. #ifndef ALT_BITSTREAM_WRITER
  4099. s->pb.buf_ptr= s->ptr_lastgob;
  4100. #endif
  4101. assert(pbBufPtr(&s->pb) == s->ptr_lastgob);
  4102. }
  4103. }
  4104. if (s->avctx->rtp_callback){
  4105. int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
  4106. s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
  4107. }
  4108. switch(s->codec_id){
  4109. case CODEC_ID_MPEG4:
  4110. ff_mpeg4_encode_video_packet_header(s);
  4111. ff_mpeg4_clean_buffers(s);
  4112. break;
  4113. case CODEC_ID_MPEG1VIDEO:
  4114. case CODEC_ID_MPEG2VIDEO:
  4115. ff_mpeg1_encode_slice_header(s);
  4116. ff_mpeg1_clean_buffers(s);
  4117. break;
  4118. case CODEC_ID_H263:
  4119. case CODEC_ID_H263P:
  4120. h263_encode_gob_header(s, mb_y);
  4121. break;
  4122. }
  4123. if(s->flags&CODEC_FLAG_PASS1){
  4124. int bits= put_bits_count(&s->pb);
  4125. s->misc_bits+= bits - s->last_bits;
  4126. s->last_bits= bits;
  4127. }
  4128. s->ptr_lastgob += current_packet_size;
  4129. s->first_slice_line=1;
  4130. s->resync_mb_x=mb_x;
  4131. s->resync_mb_y=mb_y;
  4132. }
  4133. }
  4134. #endif
  4135. if( (s->resync_mb_x == s->mb_x)
  4136. && s->resync_mb_y+1 == s->mb_y){
  4137. s->first_slice_line=0;
  4138. }
  4139. s->mb_skiped=0;
  4140. s->dquant=0; //only for QP_RD
  4141. if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){ // more than 1 MB type possible or CODEC_FLAG_QP_RD
  4142. int next_block=0;
  4143. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  4144. copy_context_before_encode(&backup_s, s, -1);
  4145. backup_s.pb= s->pb;
  4146. best_s.data_partitioning= s->data_partitioning;
  4147. best_s.partitioned_frame= s->partitioned_frame;
  4148. if(s->data_partitioning){
  4149. backup_s.pb2= s->pb2;
  4150. backup_s.tex_pb= s->tex_pb;
  4151. }
  4152. if(mb_type&CANDIDATE_MB_TYPE_INTER){
  4153. s->mv_dir = MV_DIR_FORWARD;
  4154. s->mv_type = MV_TYPE_16X16;
  4155. s->mb_intra= 0;
  4156. s->mv[0][0][0] = s->p_mv_table[xy][0];
  4157. s->mv[0][0][1] = s->p_mv_table[xy][1];
  4158. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
  4159. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  4160. }
  4161. if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
  4162. s->mv_dir = MV_DIR_FORWARD;
  4163. s->mv_type = MV_TYPE_FIELD;
  4164. s->mb_intra= 0;
  4165. for(i=0; i<2; i++){
  4166. j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  4167. s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  4168. s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  4169. }
  4170. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
  4171. &dmin, &next_block, 0, 0);
  4172. }
  4173. if(mb_type&CANDIDATE_MB_TYPE_SKIPED){
  4174. s->mv_dir = MV_DIR_FORWARD;
  4175. s->mv_type = MV_TYPE_16X16;
  4176. s->mb_intra= 0;
  4177. s->mv[0][0][0] = 0;
  4178. s->mv[0][0][1] = 0;
  4179. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPED, pb, pb2, tex_pb,
  4180. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  4181. }
  4182. if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
  4183. s->mv_dir = MV_DIR_FORWARD;
  4184. s->mv_type = MV_TYPE_8X8;
  4185. s->mb_intra= 0;
  4186. for(i=0; i<4; i++){
  4187. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  4188. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  4189. }
  4190. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
  4191. &dmin, &next_block, 0, 0);
  4192. }
  4193. if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
  4194. s->mv_dir = MV_DIR_FORWARD;
  4195. s->mv_type = MV_TYPE_16X16;
  4196. s->mb_intra= 0;
  4197. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  4198. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  4199. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
  4200. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  4201. }
  4202. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
  4203. s->mv_dir = MV_DIR_BACKWARD;
  4204. s->mv_type = MV_TYPE_16X16;
  4205. s->mb_intra= 0;
  4206. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  4207. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  4208. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  4209. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  4210. }
  4211. if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
  4212. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  4213. s->mv_type = MV_TYPE_16X16;
  4214. s->mb_intra= 0;
  4215. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  4216. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  4217. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  4218. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  4219. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
  4220. &dmin, &next_block, 0, 0);
  4221. }
  4222. if(mb_type&CANDIDATE_MB_TYPE_DIRECT){
  4223. int mx= s->b_direct_mv_table[xy][0];
  4224. int my= s->b_direct_mv_table[xy][1];
  4225. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  4226. s->mb_intra= 0;
  4227. #ifdef CONFIG_RISKY
  4228. ff_mpeg4_set_direct_mv(s, mx, my);
  4229. #endif
  4230. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
  4231. &dmin, &next_block, mx, my);
  4232. }
  4233. if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
  4234. s->mv_dir = MV_DIR_FORWARD;
  4235. s->mv_type = MV_TYPE_FIELD;
  4236. s->mb_intra= 0;
  4237. for(i=0; i<2; i++){
  4238. j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  4239. s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  4240. s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  4241. }
  4242. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
  4243. &dmin, &next_block, 0, 0);
  4244. }
  4245. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
  4246. s->mv_dir = MV_DIR_BACKWARD;
  4247. s->mv_type = MV_TYPE_FIELD;
  4248. s->mb_intra= 0;
  4249. for(i=0; i<2; i++){
  4250. j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  4251. s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  4252. s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  4253. }
  4254. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
  4255. &dmin, &next_block, 0, 0);
  4256. }
  4257. if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
  4258. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  4259. s->mv_type = MV_TYPE_FIELD;
  4260. s->mb_intra= 0;
  4261. for(dir=0; dir<2; dir++){
  4262. for(i=0; i<2; i++){
  4263. j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  4264. s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  4265. s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  4266. }
  4267. }
  4268. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
  4269. &dmin, &next_block, 0, 0);
  4270. }
  4271. if(mb_type&CANDIDATE_MB_TYPE_INTRA){
  4272. s->mv_dir = 0;
  4273. s->mv_type = MV_TYPE_16X16;
  4274. s->mb_intra= 1;
  4275. s->mv[0][0][0] = 0;
  4276. s->mv[0][0][1] = 0;
  4277. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
  4278. &dmin, &next_block, 0, 0);
  4279. if(s->h263_pred || s->h263_aic){
  4280. if(best_s.mb_intra)
  4281. s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
  4282. else
  4283. ff_clean_intra_table_entries(s); //old mode?
  4284. }
  4285. }
  4286. if(s->flags & CODEC_FLAG_QP_RD){
  4287. if(best_s.mv_type==MV_TYPE_16X16 && !(best_s.mv_dir&MV_DIRECT)){
  4288. const int last_qp= backup_s.qscale;
  4289. int dquant, dir, qp, dc[6];
  4290. DCTELEM ac[6][16];
  4291. const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
  4292. assert(backup_s.dquant == 0);
  4293. //FIXME intra
  4294. s->mv_dir= best_s.mv_dir;
  4295. s->mv_type = MV_TYPE_16X16;
  4296. s->mb_intra= best_s.mb_intra;
  4297. s->mv[0][0][0] = best_s.mv[0][0][0];
  4298. s->mv[0][0][1] = best_s.mv[0][0][1];
  4299. s->mv[1][0][0] = best_s.mv[1][0][0];
  4300. s->mv[1][0][1] = best_s.mv[1][0][1];
  4301. dir= s->pict_type == B_TYPE ? 2 : 1;
  4302. if(last_qp + dir > s->avctx->qmax) dir= -dir;
  4303. for(dquant= dir; dquant<=2 && dquant>=-2; dquant += dir){
  4304. qp= last_qp + dquant;
  4305. if(qp < s->avctx->qmin || qp > s->avctx->qmax)
  4306. break;
  4307. backup_s.dquant= dquant;
  4308. if(s->mb_intra && s->dc_val[0]){
  4309. for(i=0; i<6; i++){
  4310. dc[i]= s->dc_val[0][ s->block_index[i] ];
  4311. memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
  4312. }
  4313. }
  4314. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
  4315. &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
  4316. if(best_s.qscale != qp){
  4317. if(s->mb_intra && s->dc_val[0]){
  4318. for(i=0; i<6; i++){
  4319. s->dc_val[0][ s->block_index[i] ]= dc[i];
  4320. memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
  4321. }
  4322. }
  4323. if(dir > 0 && dquant==dir){
  4324. dquant= 0;
  4325. dir= -dir;
  4326. }else
  4327. break;
  4328. }
  4329. }
  4330. qp= best_s.qscale;
  4331. s->current_picture.qscale_table[xy]= qp;
  4332. }
  4333. }
  4334. copy_context_after_encode(s, &best_s, -1);
  4335. pb_bits_count= put_bits_count(&s->pb);
  4336. flush_put_bits(&s->pb);
  4337. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  4338. s->pb= backup_s.pb;
  4339. if(s->data_partitioning){
  4340. pb2_bits_count= put_bits_count(&s->pb2);
  4341. flush_put_bits(&s->pb2);
  4342. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  4343. s->pb2= backup_s.pb2;
  4344. tex_pb_bits_count= put_bits_count(&s->tex_pb);
  4345. flush_put_bits(&s->tex_pb);
  4346. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  4347. s->tex_pb= backup_s.tex_pb;
  4348. }
  4349. s->last_bits= put_bits_count(&s->pb);
  4350. #ifdef CONFIG_RISKY
  4351. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
  4352. ff_h263_update_motion_val(s);
  4353. #endif
  4354. if(next_block==0){ //FIXME 16 vs linesize16
  4355. s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16);
  4356. s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
  4357. s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
  4358. }
  4359. if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
  4360. MPV_decode_mb(s, s->block);
  4361. } else {
  4362. int motion_x, motion_y;
  4363. s->mv_type=MV_TYPE_16X16;
  4364. // only one MB-Type possible
  4365. switch(mb_type){
  4366. case CANDIDATE_MB_TYPE_INTRA:
  4367. s->mv_dir = 0;
  4368. s->mb_intra= 1;
  4369. motion_x= s->mv[0][0][0] = 0;
  4370. motion_y= s->mv[0][0][1] = 0;
  4371. break;
  4372. case CANDIDATE_MB_TYPE_INTER:
  4373. s->mv_dir = MV_DIR_FORWARD;
  4374. s->mb_intra= 0;
  4375. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  4376. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  4377. break;
  4378. case CANDIDATE_MB_TYPE_INTER_I:
  4379. s->mv_dir = MV_DIR_FORWARD;
  4380. s->mv_type = MV_TYPE_FIELD;
  4381. s->mb_intra= 0;
  4382. for(i=0; i<2; i++){
  4383. j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  4384. s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  4385. s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  4386. }
  4387. motion_x = motion_y = 0;
  4388. break;
  4389. case CANDIDATE_MB_TYPE_INTER4V:
  4390. s->mv_dir = MV_DIR_FORWARD;
  4391. s->mv_type = MV_TYPE_8X8;
  4392. s->mb_intra= 0;
  4393. for(i=0; i<4; i++){
  4394. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  4395. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  4396. }
  4397. motion_x= motion_y= 0;
  4398. break;
  4399. case CANDIDATE_MB_TYPE_DIRECT:
  4400. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  4401. s->mb_intra= 0;
  4402. motion_x=s->b_direct_mv_table[xy][0];
  4403. motion_y=s->b_direct_mv_table[xy][1];
  4404. #ifdef CONFIG_RISKY
  4405. ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
  4406. #endif
  4407. break;
  4408. case CANDIDATE_MB_TYPE_BIDIR:
  4409. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  4410. s->mb_intra= 0;
  4411. motion_x=0;
  4412. motion_y=0;
  4413. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  4414. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  4415. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  4416. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  4417. break;
  4418. case CANDIDATE_MB_TYPE_BACKWARD:
  4419. s->mv_dir = MV_DIR_BACKWARD;
  4420. s->mb_intra= 0;
  4421. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  4422. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  4423. break;
  4424. case CANDIDATE_MB_TYPE_FORWARD:
  4425. s->mv_dir = MV_DIR_FORWARD;
  4426. s->mb_intra= 0;
  4427. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  4428. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  4429. // printf(" %d %d ", motion_x, motion_y);
  4430. break;
  4431. case CANDIDATE_MB_TYPE_FORWARD_I:
  4432. s->mv_dir = MV_DIR_FORWARD;
  4433. s->mv_type = MV_TYPE_FIELD;
  4434. s->mb_intra= 0;
  4435. for(i=0; i<2; i++){
  4436. j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  4437. s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  4438. s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  4439. }
  4440. motion_x=motion_y=0;
  4441. break;
  4442. case CANDIDATE_MB_TYPE_BACKWARD_I:
  4443. s->mv_dir = MV_DIR_BACKWARD;
  4444. s->mv_type = MV_TYPE_FIELD;
  4445. s->mb_intra= 0;
  4446. for(i=0; i<2; i++){
  4447. j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  4448. s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  4449. s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  4450. }
  4451. motion_x=motion_y=0;
  4452. break;
  4453. case CANDIDATE_MB_TYPE_BIDIR_I:
  4454. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  4455. s->mv_type = MV_TYPE_FIELD;
  4456. s->mb_intra= 0;
  4457. for(dir=0; dir<2; dir++){
  4458. for(i=0; i<2; i++){
  4459. j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  4460. s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  4461. s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  4462. }
  4463. }
  4464. motion_x=motion_y=0;
  4465. break;
  4466. default:
  4467. motion_x=motion_y=0; //gcc warning fix
  4468. av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
  4469. }
  4470. encode_mb(s, motion_x, motion_y);
  4471. // RAL: Update last macrobloc type
  4472. s->last_mv_dir = s->mv_dir;
  4473. #ifdef CONFIG_RISKY
  4474. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
  4475. ff_h263_update_motion_val(s);
  4476. #endif
  4477. MPV_decode_mb(s, s->block);
  4478. }
  4479. /* clean the MV table in IPS frames for direct mode in B frames */
  4480. if(s->mb_intra /* && I,P,S_TYPE */){
  4481. s->p_mv_table[xy][0]=0;
  4482. s->p_mv_table[xy][1]=0;
  4483. }
  4484. if(s->flags&CODEC_FLAG_PSNR){
  4485. int w= 16;
  4486. int h= 16;
  4487. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  4488. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  4489. s->current_picture_ptr->error[0] += sse(
  4490. s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  4491. s->dest[0], w, h, s->linesize);
  4492. s->current_picture_ptr->error[1] += sse(
  4493. s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  4494. s->dest[1], w>>1, h>>1, s->uvlinesize);
  4495. s->current_picture_ptr->error[2] += sse(
  4496. s, s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  4497. s->dest[2], w>>1, h>>1, s->uvlinesize);
  4498. }
  4499. if(s->loop_filter){
  4500. if(s->out_format == FMT_H263)
  4501. ff_h263_loop_filter(s);
  4502. }
  4503. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, put_bits_count(&s->pb));
  4504. }
  4505. }
  4506. #ifdef CONFIG_RISKY
  4507. //not beautifull here but we must write it before flushing so it has to be here
  4508. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  4509. msmpeg4_encode_ext_header(s);
  4510. #endif
  4511. write_slice_end(s);
  4512. /* Send the last GOB if RTP */
  4513. if (s->avctx->rtp_callback) {
  4514. int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
  4515. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  4516. /* Call the RTP callback to send the last GOB */
  4517. emms_c();
  4518. s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
  4519. }
  4520. return 0;
  4521. }
  4522. #define MERGE(field) dst->field += src->field; src->field=0
  4523. static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
  4524. MERGE(me.scene_change_score);
  4525. MERGE(me.mc_mb_var_sum_temp);
  4526. MERGE(me.mb_var_sum_temp);
  4527. }
  4528. static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
  4529. int i;
  4530. MERGE(dct_count[0]); //note, the other dct vars are not part of the context
  4531. MERGE(dct_count[1]);
  4532. MERGE(mv_bits);
  4533. MERGE(i_tex_bits);
  4534. MERGE(p_tex_bits);
  4535. MERGE(i_count);
  4536. MERGE(f_count);
  4537. MERGE(b_count);
  4538. MERGE(skip_count);
  4539. MERGE(misc_bits);
  4540. MERGE(error_count);
  4541. MERGE(padding_bug_score);
  4542. if(dst->avctx->noise_reduction){
  4543. for(i=0; i<64; i++){
  4544. MERGE(dct_error_sum[0][i]);
  4545. MERGE(dct_error_sum[1][i]);
  4546. }
  4547. }
  4548. assert(put_bits_count(&src->pb) % 8 ==0);
  4549. assert(put_bits_count(&dst->pb) % 8 ==0);
  4550. ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
  4551. flush_put_bits(&dst->pb);
  4552. }
  4553. static void encode_picture(MpegEncContext *s, int picture_number)
  4554. {
  4555. int i;
  4556. int bits;
  4557. s->picture_number = picture_number;
  4558. /* Reset the average MB variance */
  4559. s->me.mb_var_sum_temp =
  4560. s->me.mc_mb_var_sum_temp = 0;
  4561. #ifdef CONFIG_RISKY
  4562. /* we need to initialize some time vars before we can encode b-frames */
  4563. // RAL: Condition added for MPEG1VIDEO
  4564. if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
  4565. ff_set_mpeg4_time(s, s->picture_number); //FIXME rename and use has_b_frames or similar
  4566. #endif
  4567. s->me.scene_change_score=0;
  4568. // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME ratedistoration
  4569. if(s->pict_type==I_TYPE){
  4570. if(s->msmpeg4_version >= 3) s->no_rounding=1;
  4571. else s->no_rounding=0;
  4572. }else if(s->pict_type!=B_TYPE){
  4573. if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
  4574. s->no_rounding ^= 1;
  4575. }
  4576. s->mb_intra=0; //for the rate distoration & bit compare functions
  4577. for(i=1; i<s->avctx->thread_count; i++){
  4578. ff_update_duplicate_context(s->thread_context[i], s);
  4579. }
  4580. ff_init_me(s);
  4581. /* Estimate motion for every MB */
  4582. if(s->pict_type != I_TYPE){
  4583. if(s->pict_type != B_TYPE && s->avctx->me_threshold==0){
  4584. if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
  4585. s->avctx->execute(s->avctx, pre_estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
  4586. }
  4587. }
  4588. s->avctx->execute(s->avctx, estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
  4589. }else /* if(s->pict_type == I_TYPE) */{
  4590. /* I-Frame */
  4591. for(i=0; i<s->mb_stride*s->mb_height; i++)
  4592. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  4593. if(!s->fixed_qscale){
  4594. /* finding spatial complexity for I-frame rate control */
  4595. s->avctx->execute(s->avctx, mb_var_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
  4596. }
  4597. }
  4598. for(i=1; i<s->avctx->thread_count; i++){
  4599. merge_context_after_me(s, s->thread_context[i]);
  4600. }
  4601. s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
  4602. s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
  4603. emms_c();
  4604. if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == P_TYPE){
  4605. s->pict_type= I_TYPE;
  4606. for(i=0; i<s->mb_stride*s->mb_height; i++)
  4607. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  4608. //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  4609. }
  4610. if(!s->umvplus){
  4611. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
  4612. s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
  4613. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  4614. int a,b;
  4615. a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
  4616. b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
  4617. s->f_code= FFMAX(s->f_code, FFMAX(a,b));
  4618. }
  4619. ff_fix_long_p_mvs(s);
  4620. ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
  4621. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  4622. int j;
  4623. for(i=0; i<2; i++){
  4624. for(j=0; j<2; j++)
  4625. ff_fix_long_mvs(s, s->p_field_select_table[i], j,
  4626. s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
  4627. }
  4628. }
  4629. }
  4630. if(s->pict_type==B_TYPE){
  4631. int a, b;
  4632. a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
  4633. b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  4634. s->f_code = FFMAX(a, b);
  4635. a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
  4636. b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  4637. s->b_code = FFMAX(a, b);
  4638. ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
  4639. ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
  4640. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  4641. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  4642. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  4643. int dir, j;
  4644. for(dir=0; dir<2; dir++){
  4645. for(i=0; i<2; i++){
  4646. for(j=0; j<2; j++){
  4647. int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
  4648. : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
  4649. ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
  4650. s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
  4651. }
  4652. }
  4653. }
  4654. }
  4655. }
  4656. }
  4657. if (!s->fixed_qscale)
  4658. s->current_picture.quality = ff_rate_estimate_qscale(s); //FIXME pic_ptr
  4659. if(s->adaptive_quant){
  4660. #ifdef CONFIG_RISKY
  4661. switch(s->codec_id){
  4662. case CODEC_ID_MPEG4:
  4663. ff_clean_mpeg4_qscales(s);
  4664. break;
  4665. case CODEC_ID_H263:
  4666. case CODEC_ID_H263P:
  4667. case CODEC_ID_FLV1:
  4668. ff_clean_h263_qscales(s);
  4669. break;
  4670. }
  4671. #endif
  4672. s->lambda= s->lambda_table[0];
  4673. //FIXME broken
  4674. }else
  4675. s->lambda= s->current_picture.quality;
  4676. //printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
  4677. update_qscale(s);
  4678. if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE))
  4679. s->qscale= 3; //reduce cliping problems
  4680. if (s->out_format == FMT_MJPEG) {
  4681. /* for mjpeg, we do include qscale in the matrix */
  4682. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  4683. for(i=1;i<64;i++){
  4684. int j= s->dsp.idct_permutation[i];
  4685. s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  4686. }
  4687. convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
  4688. s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
  4689. s->qscale= 8;
  4690. }
  4691. //FIXME var duplication
  4692. s->current_picture_ptr->key_frame=
  4693. s->current_picture.key_frame= s->pict_type == I_TYPE; //FIXME pic_ptr
  4694. s->current_picture_ptr->pict_type=
  4695. s->current_picture.pict_type= s->pict_type;
  4696. if(s->current_picture.key_frame)
  4697. s->picture_in_gop_number=0;
  4698. s->last_bits= put_bits_count(&s->pb);
  4699. switch(s->out_format) {
  4700. case FMT_MJPEG:
  4701. mjpeg_picture_header(s);
  4702. break;
  4703. #ifdef CONFIG_RISKY
  4704. case FMT_H261:
  4705. ff_h261_encode_picture_header(s, picture_number);
  4706. break;
  4707. case FMT_H263:
  4708. if (s->codec_id == CODEC_ID_WMV2)
  4709. ff_wmv2_encode_picture_header(s, picture_number);
  4710. else if (s->h263_msmpeg4)
  4711. msmpeg4_encode_picture_header(s, picture_number);
  4712. else if (s->h263_pred)
  4713. mpeg4_encode_picture_header(s, picture_number);
  4714. else if (s->codec_id == CODEC_ID_RV10)
  4715. rv10_encode_picture_header(s, picture_number);
  4716. else if (s->codec_id == CODEC_ID_RV20)
  4717. rv20_encode_picture_header(s, picture_number);
  4718. else if (s->codec_id == CODEC_ID_FLV1)
  4719. ff_flv_encode_picture_header(s, picture_number);
  4720. else
  4721. h263_encode_picture_header(s, picture_number);
  4722. break;
  4723. #endif
  4724. case FMT_MPEG1:
  4725. mpeg1_encode_picture_header(s, picture_number);
  4726. break;
  4727. case FMT_H264:
  4728. break;
  4729. default:
  4730. assert(0);
  4731. }
  4732. bits= put_bits_count(&s->pb);
  4733. s->header_bits= bits - s->last_bits;
  4734. for(i=1; i<s->avctx->thread_count; i++){
  4735. update_duplicate_context_after_me(s->thread_context[i], s);
  4736. }
  4737. s->avctx->execute(s->avctx, encode_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
  4738. for(i=1; i<s->avctx->thread_count; i++){
  4739. merge_context_after_encode(s, s->thread_context[i]);
  4740. }
  4741. emms_c();
  4742. }
  4743. #endif //CONFIG_ENCODERS
  4744. static void denoise_dct_c(MpegEncContext *s, DCTELEM *block){
  4745. const int intra= s->mb_intra;
  4746. int i;
  4747. s->dct_count[intra]++;
  4748. for(i=0; i<64; i++){
  4749. int level= block[i];
  4750. if(level){
  4751. if(level>0){
  4752. s->dct_error_sum[intra][i] += level;
  4753. level -= s->dct_offset[intra][i];
  4754. if(level<0) level=0;
  4755. }else{
  4756. s->dct_error_sum[intra][i] -= level;
  4757. level += s->dct_offset[intra][i];
  4758. if(level>0) level=0;
  4759. }
  4760. block[i]= level;
  4761. }
  4762. }
  4763. }
  4764. #ifdef CONFIG_ENCODERS
  4765. static int dct_quantize_trellis_c(MpegEncContext *s,
  4766. DCTELEM *block, int n,
  4767. int qscale, int *overflow){
  4768. const int *qmat;
  4769. const uint8_t *scantable= s->intra_scantable.scantable;
  4770. const uint8_t *perm_scantable= s->intra_scantable.permutated;
  4771. int max=0;
  4772. unsigned int threshold1, threshold2;
  4773. int bias=0;
  4774. int run_tab[65];
  4775. int level_tab[65];
  4776. int score_tab[65];
  4777. int survivor[65];
  4778. int survivor_count;
  4779. int last_run=0;
  4780. int last_level=0;
  4781. int last_score= 0;
  4782. int last_i;
  4783. int coeff[2][64];
  4784. int coeff_count[64];
  4785. int qmul, qadd, start_i, last_non_zero, i, dc;
  4786. const int esc_length= s->ac_esc_length;
  4787. uint8_t * length;
  4788. uint8_t * last_length;
  4789. const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
  4790. s->dsp.fdct (block);
  4791. if(s->dct_error_sum)
  4792. s->denoise_dct(s, block);
  4793. qmul= qscale*16;
  4794. qadd= ((qscale-1)|1)*8;
  4795. if (s->mb_intra) {
  4796. int q;
  4797. if (!s->h263_aic) {
  4798. if (n < 4)
  4799. q = s->y_dc_scale;
  4800. else
  4801. q = s->c_dc_scale;
  4802. q = q << 3;
  4803. } else{
  4804. /* For AIC we skip quant/dequant of INTRADC */
  4805. q = 1 << 3;
  4806. qadd=0;
  4807. }
  4808. /* note: block[0] is assumed to be positive */
  4809. block[0] = (block[0] + (q >> 1)) / q;
  4810. start_i = 1;
  4811. last_non_zero = 0;
  4812. qmat = s->q_intra_matrix[qscale];
  4813. if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  4814. bias= 1<<(QMAT_SHIFT-1);
  4815. length = s->intra_ac_vlc_length;
  4816. last_length= s->intra_ac_vlc_last_length;
  4817. } else {
  4818. start_i = 0;
  4819. last_non_zero = -1;
  4820. qmat = s->q_inter_matrix[qscale];
  4821. length = s->inter_ac_vlc_length;
  4822. last_length= s->inter_ac_vlc_last_length;
  4823. }
  4824. last_i= start_i;
  4825. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  4826. threshold2= (threshold1<<1);
  4827. for(i=63; i>=start_i; i--) {
  4828. const int j = scantable[i];
  4829. int level = block[j] * qmat[j];
  4830. if(((unsigned)(level+threshold1))>threshold2){
  4831. last_non_zero = i;
  4832. break;
  4833. }
  4834. }
  4835. for(i=start_i; i<=last_non_zero; i++) {
  4836. const int j = scantable[i];
  4837. int level = block[j] * qmat[j];
  4838. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  4839. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  4840. if(((unsigned)(level+threshold1))>threshold2){
  4841. if(level>0){
  4842. level= (bias + level)>>QMAT_SHIFT;
  4843. coeff[0][i]= level;
  4844. coeff[1][i]= level-1;
  4845. // coeff[2][k]= level-2;
  4846. }else{
  4847. level= (bias - level)>>QMAT_SHIFT;
  4848. coeff[0][i]= -level;
  4849. coeff[1][i]= -level+1;
  4850. // coeff[2][k]= -level+2;
  4851. }
  4852. coeff_count[i]= FFMIN(level, 2);
  4853. assert(coeff_count[i]);
  4854. max |=level;
  4855. }else{
  4856. coeff[0][i]= (level>>31)|1;
  4857. coeff_count[i]= 1;
  4858. }
  4859. }
  4860. *overflow= s->max_qcoeff < max; //overflow might have happend
  4861. if(last_non_zero < start_i){
  4862. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  4863. return last_non_zero;
  4864. }
  4865. score_tab[start_i]= 0;
  4866. survivor[0]= start_i;
  4867. survivor_count= 1;
  4868. for(i=start_i; i<=last_non_zero; i++){
  4869. int level_index, j;
  4870. const int dct_coeff= ABS(block[ scantable[i] ]);
  4871. const int zero_distoration= dct_coeff*dct_coeff;
  4872. int best_score=256*256*256*120;
  4873. for(level_index=0; level_index < coeff_count[i]; level_index++){
  4874. int distoration;
  4875. int level= coeff[level_index][i];
  4876. const int alevel= ABS(level);
  4877. int unquant_coeff;
  4878. assert(level);
  4879. if(s->out_format == FMT_H263){
  4880. unquant_coeff= alevel*qmul + qadd;
  4881. }else{ //MPEG1
  4882. j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize
  4883. if(s->mb_intra){
  4884. unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
  4885. unquant_coeff = (unquant_coeff - 1) | 1;
  4886. }else{
  4887. unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  4888. unquant_coeff = (unquant_coeff - 1) | 1;
  4889. }
  4890. unquant_coeff<<= 3;
  4891. }
  4892. distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distoration;
  4893. level+=64;
  4894. if((level&(~127)) == 0){
  4895. for(j=survivor_count-1; j>=0; j--){
  4896. int run= i - survivor[j];
  4897. int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  4898. score += score_tab[i-run];
  4899. if(score < best_score){
  4900. best_score= score;
  4901. run_tab[i+1]= run;
  4902. level_tab[i+1]= level-64;
  4903. }
  4904. }
  4905. if(s->out_format == FMT_H263){
  4906. for(j=survivor_count-1; j>=0; j--){
  4907. int run= i - survivor[j];
  4908. int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  4909. score += score_tab[i-run];
  4910. if(score < last_score){
  4911. last_score= score;
  4912. last_run= run;
  4913. last_level= level-64;
  4914. last_i= i+1;
  4915. }
  4916. }
  4917. }
  4918. }else{
  4919. distoration += esc_length*lambda;
  4920. for(j=survivor_count-1; j>=0; j--){
  4921. int run= i - survivor[j];
  4922. int score= distoration + score_tab[i-run];
  4923. if(score < best_score){
  4924. best_score= score;
  4925. run_tab[i+1]= run;
  4926. level_tab[i+1]= level-64;
  4927. }
  4928. }
  4929. if(s->out_format == FMT_H263){
  4930. for(j=survivor_count-1; j>=0; j--){
  4931. int run= i - survivor[j];
  4932. int score= distoration + score_tab[i-run];
  4933. if(score < last_score){
  4934. last_score= score;
  4935. last_run= run;
  4936. last_level= level-64;
  4937. last_i= i+1;
  4938. }
  4939. }
  4940. }
  4941. }
  4942. }
  4943. score_tab[i+1]= best_score;
  4944. //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
  4945. if(last_non_zero <= 27){
  4946. for(; survivor_count; survivor_count--){
  4947. if(score_tab[ survivor[survivor_count-1] ] <= best_score)
  4948. break;
  4949. }
  4950. }else{
  4951. for(; survivor_count; survivor_count--){
  4952. if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
  4953. break;
  4954. }
  4955. }
  4956. survivor[ survivor_count++ ]= i+1;
  4957. }
  4958. if(s->out_format != FMT_H263){
  4959. last_score= 256*256*256*120;
  4960. for(i= survivor[0]; i<=last_non_zero + 1; i++){
  4961. int score= score_tab[i];
  4962. if(i) score += lambda*2; //FIXME exacter?
  4963. if(score < last_score){
  4964. last_score= score;
  4965. last_i= i;
  4966. last_level= level_tab[i];
  4967. last_run= run_tab[i];
  4968. }
  4969. }
  4970. }
  4971. s->coded_score[n] = last_score;
  4972. dc= ABS(block[0]);
  4973. last_non_zero= last_i - 1;
  4974. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  4975. if(last_non_zero < start_i)
  4976. return last_non_zero;
  4977. if(last_non_zero == 0 && start_i == 0){
  4978. int best_level= 0;
  4979. int best_score= dc * dc;
  4980. for(i=0; i<coeff_count[0]; i++){
  4981. int level= coeff[i][0];
  4982. int alevel= ABS(level);
  4983. int unquant_coeff, score, distortion;
  4984. if(s->out_format == FMT_H263){
  4985. unquant_coeff= (alevel*qmul + qadd)>>3;
  4986. }else{ //MPEG1
  4987. unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
  4988. unquant_coeff = (unquant_coeff - 1) | 1;
  4989. }
  4990. unquant_coeff = (unquant_coeff + 4) >> 3;
  4991. unquant_coeff<<= 3 + 3;
  4992. distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
  4993. level+=64;
  4994. if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
  4995. else score= distortion + esc_length*lambda;
  4996. if(score < best_score){
  4997. best_score= score;
  4998. best_level= level - 64;
  4999. }
  5000. }
  5001. block[0]= best_level;
  5002. s->coded_score[n] = best_score - dc*dc;
  5003. if(best_level == 0) return -1;
  5004. else return last_non_zero;
  5005. }
  5006. i= last_i;
  5007. assert(last_level);
  5008. block[ perm_scantable[last_non_zero] ]= last_level;
  5009. i -= last_run + 1;
  5010. for(; i>start_i; i -= run_tab[i] + 1){
  5011. block[ perm_scantable[i-1] ]= level_tab[i];
  5012. }
  5013. return last_non_zero;
  5014. }
  5015. //#define REFINE_STATS 1
  5016. static int16_t basis[64][64];
  5017. static void build_basis(uint8_t *perm){
  5018. int i, j, x, y;
  5019. emms_c();
  5020. for(i=0; i<8; i++){
  5021. for(j=0; j<8; j++){
  5022. for(y=0; y<8; y++){
  5023. for(x=0; x<8; x++){
  5024. double s= 0.25*(1<<BASIS_SHIFT);
  5025. int index= 8*i + j;
  5026. int perm_index= perm[index];
  5027. if(i==0) s*= sqrt(0.5);
  5028. if(j==0) s*= sqrt(0.5);
  5029. basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5)));
  5030. }
  5031. }
  5032. }
  5033. }
  5034. }
  5035. static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
  5036. DCTELEM *block, int16_t *weight, DCTELEM *orig,
  5037. int n, int qscale){
  5038. int16_t rem[64];
  5039. DCTELEM d1[64] __align16;
  5040. const int *qmat;
  5041. const uint8_t *scantable= s->intra_scantable.scantable;
  5042. const uint8_t *perm_scantable= s->intra_scantable.permutated;
  5043. // unsigned int threshold1, threshold2;
  5044. // int bias=0;
  5045. int run_tab[65];
  5046. int prev_run=0;
  5047. int prev_level=0;
  5048. int qmul, qadd, start_i, last_non_zero, i, dc;
  5049. uint8_t * length;
  5050. uint8_t * last_length;
  5051. int lambda;
  5052. int rle_index, run, q, sum;
  5053. #ifdef REFINE_STATS
  5054. static int count=0;
  5055. static int after_last=0;
  5056. static int to_zero=0;
  5057. static int from_zero=0;
  5058. static int raise=0;
  5059. static int lower=0;
  5060. static int messed_sign=0;
  5061. #endif
  5062. if(basis[0][0] == 0)
  5063. build_basis(s->dsp.idct_permutation);
  5064. qmul= qscale*2;
  5065. qadd= (qscale-1)|1;
  5066. if (s->mb_intra) {
  5067. if (!s->h263_aic) {
  5068. if (n < 4)
  5069. q = s->y_dc_scale;
  5070. else
  5071. q = s->c_dc_scale;
  5072. } else{
  5073. /* For AIC we skip quant/dequant of INTRADC */
  5074. q = 1;
  5075. qadd=0;
  5076. }
  5077. q <<= RECON_SHIFT-3;
  5078. /* note: block[0] is assumed to be positive */
  5079. dc= block[0]*q;
  5080. // block[0] = (block[0] + (q >> 1)) / q;
  5081. start_i = 1;
  5082. qmat = s->q_intra_matrix[qscale];
  5083. // if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  5084. // bias= 1<<(QMAT_SHIFT-1);
  5085. length = s->intra_ac_vlc_length;
  5086. last_length= s->intra_ac_vlc_last_length;
  5087. } else {
  5088. dc= 0;
  5089. start_i = 0;
  5090. qmat = s->q_inter_matrix[qscale];
  5091. length = s->inter_ac_vlc_length;
  5092. last_length= s->inter_ac_vlc_last_length;
  5093. }
  5094. last_non_zero = s->block_last_index[n];
  5095. #ifdef REFINE_STATS
  5096. {START_TIMER
  5097. #endif
  5098. dc += (1<<(RECON_SHIFT-1));
  5099. for(i=0; i<64; i++){
  5100. rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME use orig dirrectly insteadof copying to rem[]
  5101. }
  5102. #ifdef REFINE_STATS
  5103. STOP_TIMER("memset rem[]")}
  5104. #endif
  5105. sum=0;
  5106. for(i=0; i<64; i++){
  5107. int one= 36;
  5108. int qns=4;
  5109. int w;
  5110. w= ABS(weight[i]) + qns*one;
  5111. w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
  5112. weight[i] = w;
  5113. // w=weight[i] = (63*qns + (w/2)) / w;
  5114. assert(w>0);
  5115. assert(w<(1<<6));
  5116. sum += w*w;
  5117. }
  5118. lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
  5119. #ifdef REFINE_STATS
  5120. {START_TIMER
  5121. #endif
  5122. run=0;
  5123. rle_index=0;
  5124. for(i=start_i; i<=last_non_zero; i++){
  5125. int j= perm_scantable[i];
  5126. const int level= block[j];
  5127. int coeff;
  5128. if(level){
  5129. if(level<0) coeff= qmul*level - qadd;
  5130. else coeff= qmul*level + qadd;
  5131. run_tab[rle_index++]=run;
  5132. run=0;
  5133. s->dsp.add_8x8basis(rem, basis[j], coeff);
  5134. }else{
  5135. run++;
  5136. }
  5137. }
  5138. #ifdef REFINE_STATS
  5139. if(last_non_zero>0){
  5140. STOP_TIMER("init rem[]")
  5141. }
  5142. }
  5143. {START_TIMER
  5144. #endif
  5145. for(;;){
  5146. int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);
  5147. int best_coeff=0;
  5148. int best_change=0;
  5149. int run2, best_unquant_change=0, analyze_gradient;
  5150. #ifdef REFINE_STATS
  5151. {START_TIMER
  5152. #endif
  5153. analyze_gradient = last_non_zero > 2 || s->avctx->quantizer_noise_shaping >= 3;
  5154. if(analyze_gradient){
  5155. #ifdef REFINE_STATS
  5156. {START_TIMER
  5157. #endif
  5158. for(i=0; i<64; i++){
  5159. int w= weight[i];
  5160. d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
  5161. }
  5162. #ifdef REFINE_STATS
  5163. STOP_TIMER("rem*w*w")}
  5164. {START_TIMER
  5165. #endif
  5166. s->dsp.fdct(d1);
  5167. #ifdef REFINE_STATS
  5168. STOP_TIMER("dct")}
  5169. #endif
  5170. }
  5171. if(start_i){
  5172. const int level= block[0];
  5173. int change, old_coeff;
  5174. assert(s->mb_intra);
  5175. old_coeff= q*level;
  5176. for(change=-1; change<=1; change+=2){
  5177. int new_level= level + change;
  5178. int score, new_coeff;
  5179. new_coeff= q*new_level;
  5180. if(new_coeff >= 2048 || new_coeff < 0)
  5181. continue;
  5182. score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
  5183. if(score<best_score){
  5184. best_score= score;
  5185. best_coeff= 0;
  5186. best_change= change;
  5187. best_unquant_change= new_coeff - old_coeff;
  5188. }
  5189. }
  5190. }
  5191. run=0;
  5192. rle_index=0;
  5193. run2= run_tab[rle_index++];
  5194. prev_level=0;
  5195. prev_run=0;
  5196. for(i=start_i; i<64; i++){
  5197. int j= perm_scantable[i];
  5198. const int level= block[j];
  5199. int change, old_coeff;
  5200. if(s->avctx->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
  5201. break;
  5202. if(level){
  5203. if(level<0) old_coeff= qmul*level - qadd;
  5204. else old_coeff= qmul*level + qadd;
  5205. run2= run_tab[rle_index++]; //FIXME ! maybe after last
  5206. }else{
  5207. old_coeff=0;
  5208. run2--;
  5209. assert(run2>=0 || i >= last_non_zero );
  5210. }
  5211. for(change=-1; change<=1; change+=2){
  5212. int new_level= level + change;
  5213. int score, new_coeff, unquant_change;
  5214. score=0;
  5215. if(s->avctx->quantizer_noise_shaping < 2 && ABS(new_level) > ABS(level))
  5216. continue;
  5217. if(new_level){
  5218. if(new_level<0) new_coeff= qmul*new_level - qadd;
  5219. else new_coeff= qmul*new_level + qadd;
  5220. if(new_coeff >= 2048 || new_coeff <= -2048)
  5221. continue;
  5222. //FIXME check for overflow
  5223. if(level){
  5224. if(level < 63 && level > -63){
  5225. if(i < last_non_zero)
  5226. score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
  5227. - length[UNI_AC_ENC_INDEX(run, level+64)];
  5228. else
  5229. score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
  5230. - last_length[UNI_AC_ENC_INDEX(run, level+64)];
  5231. }
  5232. }else{
  5233. assert(ABS(new_level)==1);
  5234. if(analyze_gradient){
  5235. int g= d1[ scantable[i] ];
  5236. if(g && (g^new_level) >= 0)
  5237. continue;
  5238. }
  5239. if(i < last_non_zero){
  5240. int next_i= i + run2 + 1;
  5241. int next_level= block[ perm_scantable[next_i] ] + 64;
  5242. if(next_level&(~127))
  5243. next_level= 0;
  5244. if(next_i < last_non_zero)
  5245. score += length[UNI_AC_ENC_INDEX(run, 65)]
  5246. + length[UNI_AC_ENC_INDEX(run2, next_level)]
  5247. - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
  5248. else
  5249. score += length[UNI_AC_ENC_INDEX(run, 65)]
  5250. + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
  5251. - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
  5252. }else{
  5253. score += last_length[UNI_AC_ENC_INDEX(run, 65)];
  5254. if(prev_level){
  5255. score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
  5256. - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
  5257. }
  5258. }
  5259. }
  5260. }else{
  5261. new_coeff=0;
  5262. assert(ABS(level)==1);
  5263. if(i < last_non_zero){
  5264. int next_i= i + run2 + 1;
  5265. int next_level= block[ perm_scantable[next_i] ] + 64;
  5266. if(next_level&(~127))
  5267. next_level= 0;
  5268. if(next_i < last_non_zero)
  5269. score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
  5270. - length[UNI_AC_ENC_INDEX(run2, next_level)]
  5271. - length[UNI_AC_ENC_INDEX(run, 65)];
  5272. else
  5273. score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
  5274. - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
  5275. - length[UNI_AC_ENC_INDEX(run, 65)];
  5276. }else{
  5277. score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
  5278. if(prev_level){
  5279. score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
  5280. - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
  5281. }
  5282. }
  5283. }
  5284. score *= lambda;
  5285. unquant_change= new_coeff - old_coeff;
  5286. assert((score < 100*lambda && score > -100*lambda) || lambda==0);
  5287. score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);
  5288. if(score<best_score){
  5289. best_score= score;
  5290. best_coeff= i;
  5291. best_change= change;
  5292. best_unquant_change= unquant_change;
  5293. }
  5294. }
  5295. if(level){
  5296. prev_level= level + 64;
  5297. if(prev_level&(~127))
  5298. prev_level= 0;
  5299. prev_run= run;
  5300. run=0;
  5301. }else{
  5302. run++;
  5303. }
  5304. }
  5305. #ifdef REFINE_STATS
  5306. STOP_TIMER("iterative step")}
  5307. #endif
  5308. if(best_change){
  5309. int j= perm_scantable[ best_coeff ];
  5310. block[j] += best_change;
  5311. if(best_coeff > last_non_zero){
  5312. last_non_zero= best_coeff;
  5313. assert(block[j]);
  5314. #ifdef REFINE_STATS
  5315. after_last++;
  5316. #endif
  5317. }else{
  5318. #ifdef REFINE_STATS
  5319. if(block[j]){
  5320. if(block[j] - best_change){
  5321. if(ABS(block[j]) > ABS(block[j] - best_change)){
  5322. raise++;
  5323. }else{
  5324. lower++;
  5325. }
  5326. }else{
  5327. from_zero++;
  5328. }
  5329. }else{
  5330. to_zero++;
  5331. }
  5332. #endif
  5333. for(; last_non_zero>=start_i; last_non_zero--){
  5334. if(block[perm_scantable[last_non_zero]])
  5335. break;
  5336. }
  5337. }
  5338. #ifdef REFINE_STATS
  5339. count++;
  5340. if(256*256*256*64 % count == 0){
  5341. printf("after_last:%d to_zero:%d from_zero:%d raise:%d lower:%d sign:%d xyp:%d/%d/%d\n", after_last, to_zero, from_zero, raise, lower, messed_sign, s->mb_x, s->mb_y, s->picture_number);
  5342. }
  5343. #endif
  5344. run=0;
  5345. rle_index=0;
  5346. for(i=start_i; i<=last_non_zero; i++){
  5347. int j= perm_scantable[i];
  5348. const int level= block[j];
  5349. if(level){
  5350. run_tab[rle_index++]=run;
  5351. run=0;
  5352. }else{
  5353. run++;
  5354. }
  5355. }
  5356. s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);
  5357. }else{
  5358. break;
  5359. }
  5360. }
  5361. #ifdef REFINE_STATS
  5362. if(last_non_zero>0){
  5363. STOP_TIMER("iterative search")
  5364. }
  5365. }
  5366. #endif
  5367. return last_non_zero;
  5368. }
  5369. static int dct_quantize_c(MpegEncContext *s,
  5370. DCTELEM *block, int n,
  5371. int qscale, int *overflow)
  5372. {
  5373. int i, j, level, last_non_zero, q, start_i;
  5374. const int *qmat;
  5375. const uint8_t *scantable= s->intra_scantable.scantable;
  5376. int bias;
  5377. int max=0;
  5378. unsigned int threshold1, threshold2;
  5379. s->dsp.fdct (block);
  5380. if(s->dct_error_sum)
  5381. s->denoise_dct(s, block);
  5382. if (s->mb_intra) {
  5383. if (!s->h263_aic) {
  5384. if (n < 4)
  5385. q = s->y_dc_scale;
  5386. else
  5387. q = s->c_dc_scale;
  5388. q = q << 3;
  5389. } else
  5390. /* For AIC we skip quant/dequant of INTRADC */
  5391. q = 1 << 3;
  5392. /* note: block[0] is assumed to be positive */
  5393. block[0] = (block[0] + (q >> 1)) / q;
  5394. start_i = 1;
  5395. last_non_zero = 0;
  5396. qmat = s->q_intra_matrix[qscale];
  5397. bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  5398. } else {
  5399. start_i = 0;
  5400. last_non_zero = -1;
  5401. qmat = s->q_inter_matrix[qscale];
  5402. bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  5403. }
  5404. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  5405. threshold2= (threshold1<<1);
  5406. for(i=63;i>=start_i;i--) {
  5407. j = scantable[i];
  5408. level = block[j] * qmat[j];
  5409. if(((unsigned)(level+threshold1))>threshold2){
  5410. last_non_zero = i;
  5411. break;
  5412. }else{
  5413. block[j]=0;
  5414. }
  5415. }
  5416. for(i=start_i; i<=last_non_zero; i++) {
  5417. j = scantable[i];
  5418. level = block[j] * qmat[j];
  5419. // if( bias+level >= (1<<QMAT_SHIFT)
  5420. // || bias-level >= (1<<QMAT_SHIFT)){
  5421. if(((unsigned)(level+threshold1))>threshold2){
  5422. if(level>0){
  5423. level= (bias + level)>>QMAT_SHIFT;
  5424. block[j]= level;
  5425. }else{
  5426. level= (bias - level)>>QMAT_SHIFT;
  5427. block[j]= -level;
  5428. }
  5429. max |=level;
  5430. }else{
  5431. block[j]=0;
  5432. }
  5433. }
  5434. *overflow= s->max_qcoeff < max; //overflow might have happend
  5435. /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  5436. if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
  5437. ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
  5438. return last_non_zero;
  5439. }
  5440. #endif //CONFIG_ENCODERS
  5441. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  5442. DCTELEM *block, int n, int qscale)
  5443. {
  5444. int i, level, nCoeffs;
  5445. const uint16_t *quant_matrix;
  5446. nCoeffs= s->block_last_index[n];
  5447. if (n < 4)
  5448. block[0] = block[0] * s->y_dc_scale;
  5449. else
  5450. block[0] = block[0] * s->c_dc_scale;
  5451. /* XXX: only mpeg1 */
  5452. quant_matrix = s->intra_matrix;
  5453. for(i=1;i<=nCoeffs;i++) {
  5454. int j= s->intra_scantable.permutated[i];
  5455. level = block[j];
  5456. if (level) {
  5457. if (level < 0) {
  5458. level = -level;
  5459. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  5460. level = (level - 1) | 1;
  5461. level = -level;
  5462. } else {
  5463. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  5464. level = (level - 1) | 1;
  5465. }
  5466. block[j] = level;
  5467. }
  5468. }
  5469. }
  5470. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  5471. DCTELEM *block, int n, int qscale)
  5472. {
  5473. int i, level, nCoeffs;
  5474. const uint16_t *quant_matrix;
  5475. nCoeffs= s->block_last_index[n];
  5476. quant_matrix = s->inter_matrix;
  5477. for(i=0; i<=nCoeffs; i++) {
  5478. int j= s->intra_scantable.permutated[i];
  5479. level = block[j];
  5480. if (level) {
  5481. if (level < 0) {
  5482. level = -level;
  5483. level = (((level << 1) + 1) * qscale *
  5484. ((int) (quant_matrix[j]))) >> 4;
  5485. level = (level - 1) | 1;
  5486. level = -level;
  5487. } else {
  5488. level = (((level << 1) + 1) * qscale *
  5489. ((int) (quant_matrix[j]))) >> 4;
  5490. level = (level - 1) | 1;
  5491. }
  5492. block[j] = level;
  5493. }
  5494. }
  5495. }
  5496. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  5497. DCTELEM *block, int n, int qscale)
  5498. {
  5499. int i, level, nCoeffs;
  5500. const uint16_t *quant_matrix;
  5501. if(s->alternate_scan) nCoeffs= 63;
  5502. else nCoeffs= s->block_last_index[n];
  5503. if (n < 4)
  5504. block[0] = block[0] * s->y_dc_scale;
  5505. else
  5506. block[0] = block[0] * s->c_dc_scale;
  5507. quant_matrix = s->intra_matrix;
  5508. for(i=1;i<=nCoeffs;i++) {
  5509. int j= s->intra_scantable.permutated[i];
  5510. level = block[j];
  5511. if (level) {
  5512. if (level < 0) {
  5513. level = -level;
  5514. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  5515. level = -level;
  5516. } else {
  5517. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  5518. }
  5519. block[j] = level;
  5520. }
  5521. }
  5522. }
  5523. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  5524. DCTELEM *block, int n, int qscale)
  5525. {
  5526. int i, level, nCoeffs;
  5527. const uint16_t *quant_matrix;
  5528. int sum=-1;
  5529. if(s->alternate_scan) nCoeffs= 63;
  5530. else nCoeffs= s->block_last_index[n];
  5531. quant_matrix = s->inter_matrix;
  5532. for(i=0; i<=nCoeffs; i++) {
  5533. int j= s->intra_scantable.permutated[i];
  5534. level = block[j];
  5535. if (level) {
  5536. if (level < 0) {
  5537. level = -level;
  5538. level = (((level << 1) + 1) * qscale *
  5539. ((int) (quant_matrix[j]))) >> 4;
  5540. level = -level;
  5541. } else {
  5542. level = (((level << 1) + 1) * qscale *
  5543. ((int) (quant_matrix[j]))) >> 4;
  5544. }
  5545. block[j] = level;
  5546. sum+=level;
  5547. }
  5548. }
  5549. block[63]^=sum&1;
  5550. }
  5551. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  5552. DCTELEM *block, int n, int qscale)
  5553. {
  5554. int i, level, qmul, qadd;
  5555. int nCoeffs;
  5556. assert(s->block_last_index[n]>=0);
  5557. qmul = qscale << 1;
  5558. if (!s->h263_aic) {
  5559. if (n < 4)
  5560. block[0] = block[0] * s->y_dc_scale;
  5561. else
  5562. block[0] = block[0] * s->c_dc_scale;
  5563. qadd = (qscale - 1) | 1;
  5564. }else{
  5565. qadd = 0;
  5566. }
  5567. if(s->ac_pred)
  5568. nCoeffs=63;
  5569. else
  5570. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  5571. for(i=1; i<=nCoeffs; i++) {
  5572. level = block[i];
  5573. if (level) {
  5574. if (level < 0) {
  5575. level = level * qmul - qadd;
  5576. } else {
  5577. level = level * qmul + qadd;
  5578. }
  5579. block[i] = level;
  5580. }
  5581. }
  5582. }
  5583. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  5584. DCTELEM *block, int n, int qscale)
  5585. {
  5586. int i, level, qmul, qadd;
  5587. int nCoeffs;
  5588. assert(s->block_last_index[n]>=0);
  5589. qadd = (qscale - 1) | 1;
  5590. qmul = qscale << 1;
  5591. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  5592. for(i=0; i<=nCoeffs; i++) {
  5593. level = block[i];
  5594. if (level) {
  5595. if (level < 0) {
  5596. level = level * qmul - qadd;
  5597. } else {
  5598. level = level * qmul + qadd;
  5599. }
  5600. block[i] = level;
  5601. }
  5602. }
  5603. }
  5604. static const AVOption mpeg4_options[] =
  5605. {
  5606. AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
  5607. AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
  5608. "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
  5609. bit_rate_tolerance, 4, 240000000, 8000),
  5610. AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
  5611. AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),
  5612. AVOPTION_CODEC_STRING("rc_eq", "rate control equation",
  5613. rc_eq, "tex^qComp,option1,options2", 0),
  5614. AVOPTION_CODEC_INT("rc_minrate", "rate control minimum bitrate",
  5615. rc_min_rate, 4, 24000000, 0),
  5616. AVOPTION_CODEC_INT("rc_maxrate", "rate control maximum bitrate",
  5617. rc_max_rate, 4, 24000000, 0),
  5618. AVOPTION_CODEC_DOUBLE("rc_buf_aggresivity", "rate control buffer aggresivity",
  5619. rc_buffer_aggressivity, 4, 24000000, 0),
  5620. AVOPTION_CODEC_DOUBLE("rc_initial_cplx", "initial complexity for pass1 ratecontrol",
  5621. rc_initial_cplx, 0., 9999999., 0),
  5622. AVOPTION_CODEC_DOUBLE("i_quant_factor", "qscale factor between p and i frames",
  5623. i_quant_factor, 0., 0., 0),
  5624. AVOPTION_CODEC_DOUBLE("i_quant_offset", "qscale offset between p and i frames",
  5625. i_quant_factor, -999999., 999999., 0),
  5626. AVOPTION_CODEC_INT("dct_algo", "dct alghorithm",
  5627. dct_algo, 0, 5, 0), // fixme - "Auto,FastInt,Int,MMX,MLib,Altivec"
  5628. AVOPTION_CODEC_DOUBLE("lumi_masking", "luminance masking",
  5629. lumi_masking, 0., 999999., 0),
  5630. AVOPTION_CODEC_DOUBLE("temporal_cplx_masking", "temporary complexity masking",
  5631. temporal_cplx_masking, 0., 999999., 0),
  5632. AVOPTION_CODEC_DOUBLE("spatial_cplx_masking", "spatial complexity masking",
  5633. spatial_cplx_masking, 0., 999999., 0),
  5634. AVOPTION_CODEC_DOUBLE("p_masking", "p block masking",
  5635. p_masking, 0., 999999., 0),
  5636. AVOPTION_CODEC_DOUBLE("dark_masking", "darkness masking",
  5637. dark_masking, 0., 999999., 0),
  5638. AVOPTION_CODEC_INT("idct_algo", "idct alghorithm",
  5639. idct_algo, 0, 8, 0), // fixme - "Auto,Int,Simple,SimpleMMX,LibMPEG2MMX,PS2,MLib,ARM,Altivec"
  5640. AVOPTION_CODEC_INT("mb_qmin", "minimum MB quantizer",
  5641. mb_qmin, 0, 8, 0),
  5642. AVOPTION_CODEC_INT("mb_qmax", "maximum MB quantizer",
  5643. mb_qmin, 0, 8, 0),
  5644. AVOPTION_CODEC_INT("me_cmp", "ME compare function",
  5645. me_cmp, 0, 24000000, 0),
  5646. AVOPTION_CODEC_INT("me_sub_cmp", "subpixel ME compare function",
  5647. me_sub_cmp, 0, 24000000, 0),
  5648. AVOPTION_CODEC_INT("dia_size", "ME diamond size & shape",
  5649. dia_size, 0, 24000000, 0),
  5650. AVOPTION_CODEC_INT("last_predictor_count", "amount of previous MV predictors",
  5651. last_predictor_count, 0, 24000000, 0),
  5652. AVOPTION_CODEC_INT("pre_me", "pre pass for ME",
  5653. pre_me, 0, 24000000, 0),
  5654. AVOPTION_CODEC_INT("me_pre_cmp", "ME pre pass compare function",
  5655. me_pre_cmp, 0, 24000000, 0),
  5656. AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  5657. me_range, 0, 24000000, 0),
  5658. AVOPTION_CODEC_INT("pre_dia_size", "ME pre pass diamod size & shape",
  5659. pre_dia_size, 0, 24000000, 0),
  5660. AVOPTION_CODEC_INT("me_subpel_quality", "subpel ME quality",
  5661. me_subpel_quality, 0, 24000000, 0),
  5662. AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  5663. me_range, 0, 24000000, 0),
  5664. AVOPTION_CODEC_FLAG("psnr", "calculate PSNR of compressed frames",
  5665. flags, CODEC_FLAG_PSNR, 0),
  5666. AVOPTION_CODEC_RCOVERRIDE("rc_override", "ratecontrol override (=startframe,endframe,qscale,quality_factor)",
  5667. rc_override),
  5668. AVOPTION_SUB(avoptions_common),
  5669. AVOPTION_END()
  5670. };
  5671. #ifdef CONFIG_ENCODERS
  5672. #ifdef CONFIG_RISKY
  5673. AVCodec h263_encoder = {
  5674. "h263",
  5675. CODEC_TYPE_VIDEO,
  5676. CODEC_ID_H263,
  5677. sizeof(MpegEncContext),
  5678. MPV_encode_init,
  5679. MPV_encode_picture,
  5680. MPV_encode_end,
  5681. };
  5682. AVCodec h263p_encoder = {
  5683. "h263p",
  5684. CODEC_TYPE_VIDEO,
  5685. CODEC_ID_H263P,
  5686. sizeof(MpegEncContext),
  5687. MPV_encode_init,
  5688. MPV_encode_picture,
  5689. MPV_encode_end,
  5690. };
  5691. AVCodec flv_encoder = {
  5692. "flv",
  5693. CODEC_TYPE_VIDEO,
  5694. CODEC_ID_FLV1,
  5695. sizeof(MpegEncContext),
  5696. MPV_encode_init,
  5697. MPV_encode_picture,
  5698. MPV_encode_end,
  5699. };
  5700. AVCodec rv10_encoder = {
  5701. "rv10",
  5702. CODEC_TYPE_VIDEO,
  5703. CODEC_ID_RV10,
  5704. sizeof(MpegEncContext),
  5705. MPV_encode_init,
  5706. MPV_encode_picture,
  5707. MPV_encode_end,
  5708. };
  5709. AVCodec rv20_encoder = {
  5710. "rv20",
  5711. CODEC_TYPE_VIDEO,
  5712. CODEC_ID_RV20,
  5713. sizeof(MpegEncContext),
  5714. MPV_encode_init,
  5715. MPV_encode_picture,
  5716. MPV_encode_end,
  5717. };
  5718. AVCodec mpeg4_encoder = {
  5719. "mpeg4",
  5720. CODEC_TYPE_VIDEO,
  5721. CODEC_ID_MPEG4,
  5722. sizeof(MpegEncContext),
  5723. MPV_encode_init,
  5724. MPV_encode_picture,
  5725. MPV_encode_end,
  5726. .options = mpeg4_options,
  5727. .capabilities= CODEC_CAP_DELAY,
  5728. };
  5729. AVCodec msmpeg4v1_encoder = {
  5730. "msmpeg4v1",
  5731. CODEC_TYPE_VIDEO,
  5732. CODEC_ID_MSMPEG4V1,
  5733. sizeof(MpegEncContext),
  5734. MPV_encode_init,
  5735. MPV_encode_picture,
  5736. MPV_encode_end,
  5737. .options = mpeg4_options,
  5738. };
  5739. AVCodec msmpeg4v2_encoder = {
  5740. "msmpeg4v2",
  5741. CODEC_TYPE_VIDEO,
  5742. CODEC_ID_MSMPEG4V2,
  5743. sizeof(MpegEncContext),
  5744. MPV_encode_init,
  5745. MPV_encode_picture,
  5746. MPV_encode_end,
  5747. .options = mpeg4_options,
  5748. };
  5749. AVCodec msmpeg4v3_encoder = {
  5750. "msmpeg4",
  5751. CODEC_TYPE_VIDEO,
  5752. CODEC_ID_MSMPEG4V3,
  5753. sizeof(MpegEncContext),
  5754. MPV_encode_init,
  5755. MPV_encode_picture,
  5756. MPV_encode_end,
  5757. .options = mpeg4_options,
  5758. };
  5759. AVCodec wmv1_encoder = {
  5760. "wmv1",
  5761. CODEC_TYPE_VIDEO,
  5762. CODEC_ID_WMV1,
  5763. sizeof(MpegEncContext),
  5764. MPV_encode_init,
  5765. MPV_encode_picture,
  5766. MPV_encode_end,
  5767. .options = mpeg4_options,
  5768. };
  5769. #endif
  5770. AVCodec mjpeg_encoder = {
  5771. "mjpeg",
  5772. CODEC_TYPE_VIDEO,
  5773. CODEC_ID_MJPEG,
  5774. sizeof(MpegEncContext),
  5775. MPV_encode_init,
  5776. MPV_encode_picture,
  5777. MPV_encode_end,
  5778. };
  5779. #endif //CONFIG_ENCODERS