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.

6565 lines
238KB

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