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.

6115 lines
219KB

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