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.

6947 lines
251KB

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