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.

6940 lines
252KB

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