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.

6539 lines
236KB

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