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.

5963 lines
212KB

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