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.

6110 lines
219KB

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