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.

6934 lines
252KB

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