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.

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