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.

6432 lines
232KB

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