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.

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