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.

6944 lines
252KB

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