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.

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