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.

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