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.

6054 lines
216KB

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