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.

6590 lines
238KB

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