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.

2426 lines
92KB

  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. * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * The simplest mpeg encoder (well, it was the simplest!).
  27. */
  28. #include "libavutil/intmath.h"
  29. #include "avcodec.h"
  30. #include "dsputil.h"
  31. #include "internal.h"
  32. #include "mpegvideo.h"
  33. #include "mpegvideo_common.h"
  34. #include "mjpegenc.h"
  35. #include "msmpeg4.h"
  36. #include "faandct.h"
  37. #include "xvmc_internal.h"
  38. #include <limits.h>
  39. //#undef NDEBUG
  40. //#include <assert.h>
  41. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  42. DCTELEM *block, int n, int qscale);
  43. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  44. DCTELEM *block, int n, int qscale);
  45. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  46. DCTELEM *block, int n, int qscale);
  47. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  48. DCTELEM *block, int n, int qscale);
  49. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  50. DCTELEM *block, int n, int qscale);
  51. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  52. DCTELEM *block, int n, int qscale);
  53. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  54. DCTELEM *block, int n, int qscale);
  55. /* enable all paranoid tests for rounding, overflows, etc... */
  56. //#define PARANOID
  57. //#define DEBUG
  58. static const uint8_t ff_default_chroma_qscale_table[32]={
  59. // 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
  60. 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
  61. };
  62. const uint8_t ff_mpeg1_dc_scale_table[128]={
  63. // 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
  64. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  65. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  66. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  67. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  68. };
  69. static const uint8_t mpeg2_dc_scale_table1[128]={
  70. // 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
  71. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  72. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  73. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  74. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  75. };
  76. static const uint8_t mpeg2_dc_scale_table2[128]={
  77. // 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
  78. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  79. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  80. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  81. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  82. };
  83. static const uint8_t mpeg2_dc_scale_table3[128]={
  84. // 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
  85. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  86. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  87. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  88. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  89. };
  90. const uint8_t * const ff_mpeg2_dc_scale_table[4]={
  91. ff_mpeg1_dc_scale_table,
  92. mpeg2_dc_scale_table1,
  93. mpeg2_dc_scale_table2,
  94. mpeg2_dc_scale_table3,
  95. };
  96. const enum PixelFormat ff_pixfmt_list_420[] = {
  97. PIX_FMT_YUV420P,
  98. PIX_FMT_NONE
  99. };
  100. const enum PixelFormat ff_hwaccel_pixfmt_list_420[] = {
  101. PIX_FMT_DXVA2_VLD,
  102. PIX_FMT_VAAPI_VLD,
  103. PIX_FMT_YUV420P,
  104. PIX_FMT_NONE
  105. };
  106. const uint8_t *ff_find_start_code(const uint8_t * restrict p, const uint8_t *end, uint32_t * restrict state){
  107. int i;
  108. assert(p<=end);
  109. if(p>=end)
  110. return end;
  111. for(i=0; i<3; i++){
  112. uint32_t tmp= *state << 8;
  113. *state= tmp + *(p++);
  114. if(tmp == 0x100 || p==end)
  115. return p;
  116. }
  117. while(p<end){
  118. if (p[-1] > 1 ) p+= 3;
  119. else if(p[-2] ) p+= 2;
  120. else if(p[-3]|(p[-1]-1)) p++;
  121. else{
  122. p++;
  123. break;
  124. }
  125. }
  126. p= FFMIN(p, end)-4;
  127. *state= AV_RB32(p);
  128. return p+4;
  129. }
  130. /* init common dct for both encoder and decoder */
  131. av_cold int ff_dct_common_init(MpegEncContext *s)
  132. {
  133. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  134. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  135. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  136. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  137. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  138. if(s->flags & CODEC_FLAG_BITEXACT)
  139. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  140. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  141. #if HAVE_MMX
  142. MPV_common_init_mmx(s);
  143. #elif ARCH_ALPHA
  144. MPV_common_init_axp(s);
  145. #elif CONFIG_MLIB
  146. MPV_common_init_mlib(s);
  147. #elif HAVE_MMI
  148. MPV_common_init_mmi(s);
  149. #elif ARCH_ARM
  150. MPV_common_init_arm(s);
  151. #elif HAVE_ALTIVEC
  152. MPV_common_init_altivec(s);
  153. #elif ARCH_BFIN
  154. MPV_common_init_bfin(s);
  155. #endif
  156. /* load & permutate scantables
  157. note: only wmv uses different ones
  158. */
  159. if(s->alternate_scan){
  160. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  161. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  162. }else{
  163. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  164. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  165. }
  166. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  167. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  168. return 0;
  169. }
  170. void ff_copy_picture(Picture *dst, Picture *src){
  171. *dst = *src;
  172. dst->type= FF_BUFFER_TYPE_COPY;
  173. }
  174. /**
  175. * Release a frame buffer
  176. */
  177. static void free_frame_buffer(MpegEncContext *s, Picture *pic)
  178. {
  179. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  180. av_freep(&pic->hwaccel_picture_private);
  181. }
  182. /**
  183. * Allocate a frame buffer
  184. */
  185. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  186. {
  187. int r;
  188. if (s->avctx->hwaccel) {
  189. assert(!pic->hwaccel_picture_private);
  190. if (s->avctx->hwaccel->priv_data_size) {
  191. pic->hwaccel_picture_private = av_mallocz(s->avctx->hwaccel->priv_data_size);
  192. if (!pic->hwaccel_picture_private) {
  193. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  194. return -1;
  195. }
  196. }
  197. }
  198. r = s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
  199. if (r<0 || !pic->age || !pic->type || !pic->data[0]) {
  200. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
  201. av_freep(&pic->hwaccel_picture_private);
  202. return -1;
  203. }
  204. if (s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])) {
  205. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
  206. free_frame_buffer(s, pic);
  207. return -1;
  208. }
  209. if (pic->linesize[1] != pic->linesize[2]) {
  210. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride mismatch)\n");
  211. free_frame_buffer(s, pic);
  212. return -1;
  213. }
  214. return 0;
  215. }
  216. /**
  217. * allocates a Picture
  218. * The pixels are allocated/set by calling get_buffer() if shared=0
  219. */
  220. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared){
  221. const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) does not sig11
  222. const int mb_array_size= s->mb_stride*s->mb_height;
  223. const int b8_array_size= s->b8_stride*s->mb_height*2;
  224. const int b4_array_size= s->b4_stride*s->mb_height*4;
  225. int i;
  226. int r= -1;
  227. if(shared){
  228. assert(pic->data[0]);
  229. assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
  230. pic->type= FF_BUFFER_TYPE_SHARED;
  231. }else{
  232. assert(!pic->data[0]);
  233. if (alloc_frame_buffer(s, pic) < 0)
  234. return -1;
  235. s->linesize = pic->linesize[0];
  236. s->uvlinesize= pic->linesize[1];
  237. }
  238. if(pic->qscale_table==NULL){
  239. if (s->encoding) {
  240. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_var , mb_array_size * sizeof(int16_t) , fail)
  241. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mc_mb_var, mb_array_size * sizeof(int16_t) , fail)
  242. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_mean , mb_array_size * sizeof(int8_t ) , fail)
  243. }
  244. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2, fail) //the +2 is for the slice end check
  245. FF_ALLOCZ_OR_GOTO(s->avctx, pic->qscale_table , mb_array_size * sizeof(uint8_t) , fail)
  246. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_type_base , (big_mb_num + s->mb_stride) * sizeof(uint32_t), fail)
  247. pic->mb_type= pic->mb_type_base + 2*s->mb_stride+1;
  248. if(s->out_format == FMT_H264){
  249. for(i=0; i<2; i++){
  250. FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i], 2 * (b4_array_size+4) * sizeof(int16_t), fail)
  251. pic->motion_val[i]= pic->motion_val_base[i]+4;
  252. FF_ALLOCZ_OR_GOTO(s->avctx, pic->ref_index[i], 4*mb_array_size * sizeof(uint8_t), fail)
  253. }
  254. pic->motion_subsample_log2= 2;
  255. }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
  256. for(i=0; i<2; i++){
  257. FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i], 2 * (b8_array_size+4) * sizeof(int16_t), fail)
  258. pic->motion_val[i]= pic->motion_val_base[i]+4;
  259. FF_ALLOCZ_OR_GOTO(s->avctx, pic->ref_index[i], 4*mb_array_size * sizeof(uint8_t), fail)
  260. }
  261. pic->motion_subsample_log2= 3;
  262. }
  263. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  264. FF_ALLOCZ_OR_GOTO(s->avctx, pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6, fail)
  265. }
  266. pic->qstride= s->mb_stride;
  267. FF_ALLOCZ_OR_GOTO(s->avctx, pic->pan_scan , 1 * sizeof(AVPanScan), fail)
  268. }
  269. /* It might be nicer if the application would keep track of these
  270. * but it would require an API change. */
  271. memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
  272. s->prev_pict_types[0]= s->dropable ? FF_B_TYPE : s->pict_type;
  273. if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == FF_B_TYPE)
  274. pic->age= INT_MAX; // Skipped MBs in B-frames are quite rare in MPEG-1/2 and it is a bit tricky to skip them anyway.
  275. return 0;
  276. fail: //for the FF_ALLOCZ_OR_GOTO macro
  277. if(r>=0)
  278. free_frame_buffer(s, pic);
  279. return -1;
  280. }
  281. /**
  282. * deallocates a picture
  283. */
  284. static void free_picture(MpegEncContext *s, Picture *pic){
  285. int i;
  286. if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
  287. free_frame_buffer(s, pic);
  288. }
  289. av_freep(&pic->mb_var);
  290. av_freep(&pic->mc_mb_var);
  291. av_freep(&pic->mb_mean);
  292. av_freep(&pic->mbskip_table);
  293. av_freep(&pic->qscale_table);
  294. av_freep(&pic->mb_type_base);
  295. av_freep(&pic->dct_coeff);
  296. av_freep(&pic->pan_scan);
  297. pic->mb_type= NULL;
  298. for(i=0; i<2; i++){
  299. av_freep(&pic->motion_val_base[i]);
  300. av_freep(&pic->ref_index[i]);
  301. }
  302. if(pic->type == FF_BUFFER_TYPE_SHARED){
  303. for(i=0; i<4; i++){
  304. pic->base[i]=
  305. pic->data[i]= NULL;
  306. }
  307. pic->type= 0;
  308. }
  309. }
  310. static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
  311. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  312. int c_size = s->mb_stride * (s->mb_height + 1);
  313. int yc_size = y_size + 2 * c_size;
  314. int i;
  315. // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
  316. FF_ALLOCZ_OR_GOTO(s->avctx, s->allocated_edge_emu_buffer, (s->width+64)*2*21*2, fail); //(width + edge + align)*interlaced*MBsize*tolerance
  317. s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*21;
  318. //FIXME should be linesize instead of s->width*2 but that is not known before get_buffer()
  319. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t), fail)
  320. s->me.temp= s->me.scratchpad;
  321. s->rd_scratchpad= s->me.scratchpad;
  322. s->b_scratchpad= s->me.scratchpad;
  323. s->obmc_scratchpad= s->me.scratchpad + 16;
  324. if (s->encoding) {
  325. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map , ME_MAP_SIZE*sizeof(uint32_t), fail)
  326. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t), fail)
  327. if(s->avctx->noise_reduction){
  328. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum, 2 * 64 * sizeof(int), fail)
  329. }
  330. }
  331. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64*12*2 * sizeof(DCTELEM), fail)
  332. s->block= s->blocks[0];
  333. for(i=0;i<12;i++){
  334. s->pblocks[i] = &s->block[i];
  335. }
  336. if (s->ac_val_base) {
  337. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base, yc_size * sizeof(int16_t) * 16, fail);
  338. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  339. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  340. s->ac_val[2] = s->ac_val[1] + c_size;
  341. }
  342. return 0;
  343. fail:
  344. return -1; //free() through MPV_common_end()
  345. }
  346. static void free_duplicate_context(MpegEncContext *s){
  347. if(s==NULL) return;
  348. av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
  349. av_freep(&s->me.scratchpad);
  350. s->me.temp=
  351. s->rd_scratchpad=
  352. s->b_scratchpad=
  353. s->obmc_scratchpad= NULL;
  354. av_freep(&s->dct_error_sum);
  355. av_freep(&s->me.map);
  356. av_freep(&s->me.score_map);
  357. av_freep(&s->blocks);
  358. av_freep(&s->ac_val_base);
  359. s->block= NULL;
  360. }
  361. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src){
  362. #define COPY(a) bak->a= src->a
  363. COPY(allocated_edge_emu_buffer);
  364. COPY(edge_emu_buffer);
  365. COPY(me.scratchpad);
  366. COPY(me.temp);
  367. COPY(rd_scratchpad);
  368. COPY(b_scratchpad);
  369. COPY(obmc_scratchpad);
  370. COPY(me.map);
  371. COPY(me.score_map);
  372. COPY(blocks);
  373. COPY(block);
  374. COPY(start_mb_y);
  375. COPY(end_mb_y);
  376. COPY(me.map_generation);
  377. COPY(pb);
  378. COPY(dct_error_sum);
  379. COPY(dct_count[0]);
  380. COPY(dct_count[1]);
  381. COPY(ac_val_base);
  382. COPY(ac_val[0]);
  383. COPY(ac_val[1]);
  384. COPY(ac_val[2]);
  385. #undef COPY
  386. }
  387. void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
  388. MpegEncContext bak;
  389. int i;
  390. //FIXME copy only needed parts
  391. //START_TIMER
  392. backup_duplicate_context(&bak, dst);
  393. memcpy(dst, src, sizeof(MpegEncContext));
  394. backup_duplicate_context(dst, &bak);
  395. for(i=0;i<12;i++){
  396. dst->pblocks[i] = &dst->block[i];
  397. }
  398. //STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  399. }
  400. /**
  401. * sets the given MpegEncContext to common defaults (same for encoding and decoding).
  402. * the changed fields will not depend upon the prior state of the MpegEncContext.
  403. */
  404. void MPV_common_defaults(MpegEncContext *s){
  405. s->y_dc_scale_table=
  406. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  407. s->chroma_qscale_table= ff_default_chroma_qscale_table;
  408. s->progressive_frame= 1;
  409. s->progressive_sequence= 1;
  410. s->picture_structure= PICT_FRAME;
  411. s->coded_picture_number = 0;
  412. s->picture_number = 0;
  413. s->input_picture_number = 0;
  414. s->picture_in_gop_number = 0;
  415. s->f_code = 1;
  416. s->b_code = 1;
  417. }
  418. /**
  419. * sets the given MpegEncContext to defaults for decoding.
  420. * the changed fields will not depend upon the prior state of the MpegEncContext.
  421. */
  422. void MPV_decode_defaults(MpegEncContext *s){
  423. MPV_common_defaults(s);
  424. }
  425. /**
  426. * init common structure for both encoder and decoder.
  427. * this assumes that some variables like width/height are already set
  428. */
  429. av_cold int MPV_common_init(MpegEncContext *s)
  430. {
  431. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y, threads;
  432. if(s->codec_id == CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  433. s->mb_height = (s->height + 31) / 32 * 2;
  434. else
  435. s->mb_height = (s->height + 15) / 16;
  436. if(s->avctx->pix_fmt == PIX_FMT_NONE){
  437. av_log(s->avctx, AV_LOG_ERROR, "decoding to PIX_FMT_NONE is not supported.\n");
  438. return -1;
  439. }
  440. if(s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height)){
  441. av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
  442. return -1;
  443. }
  444. if((s->width || s->height) && avcodec_check_dimensions(s->avctx, s->width, s->height))
  445. return -1;
  446. dsputil_init(&s->dsp, s->avctx);
  447. ff_dct_common_init(s);
  448. s->flags= s->avctx->flags;
  449. s->flags2= s->avctx->flags2;
  450. s->mb_width = (s->width + 15) / 16;
  451. s->mb_stride = s->mb_width + 1;
  452. s->b8_stride = s->mb_width*2 + 1;
  453. s->b4_stride = s->mb_width*4 + 1;
  454. mb_array_size= s->mb_height * s->mb_stride;
  455. mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
  456. /* set chroma shifts */
  457. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
  458. &(s->chroma_y_shift) );
  459. /* set default edge pos, will be overriden in decode_header if needed */
  460. s->h_edge_pos= s->mb_width*16;
  461. s->v_edge_pos= s->mb_height*16;
  462. s->mb_num = s->mb_width * s->mb_height;
  463. s->block_wrap[0]=
  464. s->block_wrap[1]=
  465. s->block_wrap[2]=
  466. s->block_wrap[3]= s->b8_stride;
  467. s->block_wrap[4]=
  468. s->block_wrap[5]= s->mb_stride;
  469. y_size = s->b8_stride * (2 * s->mb_height + 1);
  470. c_size = s->mb_stride * (s->mb_height + 1);
  471. yc_size = y_size + 2 * c_size;
  472. /* convert fourcc to upper case */
  473. s->codec_tag = ff_toupper4(s->avctx->codec_tag);
  474. s->stream_codec_tag = ff_toupper4(s->avctx->stream_codec_tag);
  475. s->avctx->coded_frame= (AVFrame*)&s->current_picture;
  476. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num+1)*sizeof(int), fail) //error ressilience code looks cleaner with this
  477. for(y=0; y<s->mb_height; y++){
  478. for(x=0; x<s->mb_width; x++){
  479. s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
  480. }
  481. }
  482. s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
  483. if (s->encoding) {
  484. /* Allocate MV tables */
  485. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  486. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  487. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  488. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  489. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  490. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  491. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  492. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  493. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  494. s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  495. s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  496. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  497. if(s->msmpeg4_version){
  498. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int), fail);
  499. }
  500. FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  501. /* Allocate MB type table */
  502. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type , mb_array_size * sizeof(uint16_t), fail) //needed for encoding
  503. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
  504. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix , 64*32 * sizeof(int), fail)
  505. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix , 64*32 * sizeof(int), fail)
  506. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t), fail)
  507. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t), fail)
  508. FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*), fail)
  509. FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*), fail)
  510. if(s->avctx->noise_reduction){
  511. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail)
  512. }
  513. }
  514. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, MAX_PICTURE_COUNT * sizeof(Picture), fail)
  515. for(i = 0; i < MAX_PICTURE_COUNT; i++) {
  516. avcodec_get_frame_defaults((AVFrame *)&s->picture[i]);
  517. }
  518. FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table, mb_array_size*sizeof(uint8_t), fail)
  519. if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
  520. /* interlaced direct mode decoding tables */
  521. for(i=0; i<2; i++){
  522. int j, k;
  523. for(j=0; j<2; j++){
  524. for(k=0; k<2; k++){
  525. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_mv_table_base[i][j][k], mv_table_size * 2 * sizeof(int16_t), fail)
  526. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
  527. }
  528. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
  529. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
  530. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]+ s->mb_stride + 1;
  531. }
  532. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
  533. }
  534. }
  535. if (s->out_format == FMT_H263) {
  536. /* ac values */
  537. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base, yc_size * sizeof(int16_t) * 16, fail);
  538. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  539. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  540. s->ac_val[2] = s->ac_val[1] + c_size;
  541. /* cbp values */
  542. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  543. s->coded_block= s->coded_block_base + s->b8_stride + 1;
  544. /* cbp, ac_pred, pred_dir */
  545. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail)
  546. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail)
  547. }
  548. if (s->h263_pred || s->h263_plus || !s->encoding) {
  549. /* dc values */
  550. //MN: we need these for error resilience of intra-frames
  551. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
  552. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  553. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  554. s->dc_val[2] = s->dc_val[1] + c_size;
  555. for(i=0;i<yc_size;i++)
  556. s->dc_val_base[i] = 1024;
  557. }
  558. /* which mb is a intra block */
  559. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  560. memset(s->mbintra_table, 1, mb_array_size);
  561. /* init macroblock skip table */
  562. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size+2, fail);
  563. //Note the +1 is for a quicker mpeg4 slice_end detection
  564. FF_ALLOCZ_OR_GOTO(s->avctx, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE, fail);
  565. s->parse_context.state= -1;
  566. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  567. s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  568. s->visualization_buffer[1] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  569. s->visualization_buffer[2] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  570. }
  571. s->context_initialized = 1;
  572. s->thread_context[0]= s;
  573. threads = s->avctx->thread_count;
  574. for(i=1; i<threads; i++){
  575. s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
  576. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  577. }
  578. for(i=0; i<threads; i++){
  579. if(init_duplicate_context(s->thread_context[i], s) < 0)
  580. goto fail;
  581. s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count;
  582. s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
  583. }
  584. return 0;
  585. fail:
  586. MPV_common_end(s);
  587. return -1;
  588. }
  589. /* init common structure for both encoder and decoder */
  590. void MPV_common_end(MpegEncContext *s)
  591. {
  592. int i, j, k;
  593. for(i=0; i<s->avctx->thread_count; i++){
  594. free_duplicate_context(s->thread_context[i]);
  595. }
  596. for(i=1; i<s->avctx->thread_count; i++){
  597. av_freep(&s->thread_context[i]);
  598. }
  599. av_freep(&s->parse_context.buffer);
  600. s->parse_context.buffer_size=0;
  601. av_freep(&s->mb_type);
  602. av_freep(&s->p_mv_table_base);
  603. av_freep(&s->b_forw_mv_table_base);
  604. av_freep(&s->b_back_mv_table_base);
  605. av_freep(&s->b_bidir_forw_mv_table_base);
  606. av_freep(&s->b_bidir_back_mv_table_base);
  607. av_freep(&s->b_direct_mv_table_base);
  608. s->p_mv_table= NULL;
  609. s->b_forw_mv_table= NULL;
  610. s->b_back_mv_table= NULL;
  611. s->b_bidir_forw_mv_table= NULL;
  612. s->b_bidir_back_mv_table= NULL;
  613. s->b_direct_mv_table= NULL;
  614. for(i=0; i<2; i++){
  615. for(j=0; j<2; j++){
  616. for(k=0; k<2; k++){
  617. av_freep(&s->b_field_mv_table_base[i][j][k]);
  618. s->b_field_mv_table[i][j][k]=NULL;
  619. }
  620. av_freep(&s->b_field_select_table[i][j]);
  621. av_freep(&s->p_field_mv_table_base[i][j]);
  622. s->p_field_mv_table[i][j]=NULL;
  623. }
  624. av_freep(&s->p_field_select_table[i]);
  625. }
  626. av_freep(&s->dc_val_base);
  627. av_freep(&s->ac_val_base);
  628. av_freep(&s->coded_block_base);
  629. av_freep(&s->mbintra_table);
  630. av_freep(&s->cbp_table);
  631. av_freep(&s->pred_dir_table);
  632. av_freep(&s->mbskip_table);
  633. av_freep(&s->prev_pict_types);
  634. av_freep(&s->bitstream_buffer);
  635. s->allocated_bitstream_buffer_size=0;
  636. av_freep(&s->avctx->stats_out);
  637. av_freep(&s->ac_stats);
  638. av_freep(&s->error_status_table);
  639. av_freep(&s->mb_index2xy);
  640. av_freep(&s->lambda_table);
  641. av_freep(&s->q_intra_matrix);
  642. av_freep(&s->q_inter_matrix);
  643. av_freep(&s->q_intra_matrix16);
  644. av_freep(&s->q_inter_matrix16);
  645. av_freep(&s->input_picture);
  646. av_freep(&s->reordered_input_picture);
  647. av_freep(&s->dct_offset);
  648. if(s->picture){
  649. for(i=0; i<MAX_PICTURE_COUNT; i++){
  650. free_picture(s, &s->picture[i]);
  651. }
  652. }
  653. av_freep(&s->picture);
  654. s->context_initialized = 0;
  655. s->last_picture_ptr=
  656. s->next_picture_ptr=
  657. s->current_picture_ptr= NULL;
  658. s->linesize= s->uvlinesize= 0;
  659. for(i=0; i<3; i++)
  660. av_freep(&s->visualization_buffer[i]);
  661. avcodec_default_free_buffers(s->avctx);
  662. }
  663. void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3])
  664. {
  665. int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
  666. uint8_t index_run[MAX_RUN+1];
  667. int last, run, level, start, end, i;
  668. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  669. if(static_store && rl->max_level[0])
  670. return;
  671. /* compute max_level[], max_run[] and index_run[] */
  672. for(last=0;last<2;last++) {
  673. if (last == 0) {
  674. start = 0;
  675. end = rl->last;
  676. } else {
  677. start = rl->last;
  678. end = rl->n;
  679. }
  680. memset(max_level, 0, MAX_RUN + 1);
  681. memset(max_run, 0, MAX_LEVEL + 1);
  682. memset(index_run, rl->n, MAX_RUN + 1);
  683. for(i=start;i<end;i++) {
  684. run = rl->table_run[i];
  685. level = rl->table_level[i];
  686. if (index_run[run] == rl->n)
  687. index_run[run] = i;
  688. if (level > max_level[run])
  689. max_level[run] = level;
  690. if (run > max_run[level])
  691. max_run[level] = run;
  692. }
  693. if(static_store)
  694. rl->max_level[last] = static_store[last];
  695. else
  696. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  697. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  698. if(static_store)
  699. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  700. else
  701. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  702. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  703. if(static_store)
  704. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  705. else
  706. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  707. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  708. }
  709. }
  710. void init_vlc_rl(RLTable *rl)
  711. {
  712. int i, q;
  713. for(q=0; q<32; q++){
  714. int qmul= q*2;
  715. int qadd= (q-1)|1;
  716. if(q==0){
  717. qmul=1;
  718. qadd=0;
  719. }
  720. for(i=0; i<rl->vlc.table_size; i++){
  721. int code= rl->vlc.table[i][0];
  722. int len = rl->vlc.table[i][1];
  723. int level, run;
  724. if(len==0){ // illegal code
  725. run= 66;
  726. level= MAX_LEVEL;
  727. }else if(len<0){ //more bits needed
  728. run= 0;
  729. level= code;
  730. }else{
  731. if(code==rl->n){ //esc
  732. run= 66;
  733. level= 0;
  734. }else{
  735. run= rl->table_run [code] + 1;
  736. level= rl->table_level[code] * qmul + qadd;
  737. if(code >= rl->last) run+=192;
  738. }
  739. }
  740. rl->rl_vlc[q][i].len= len;
  741. rl->rl_vlc[q][i].level= level;
  742. rl->rl_vlc[q][i].run= run;
  743. }
  744. }
  745. }
  746. int ff_find_unused_picture(MpegEncContext *s, int shared){
  747. int i;
  748. if(shared){
  749. for(i=0; i<MAX_PICTURE_COUNT; i++){
  750. if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
  751. }
  752. }else{
  753. for(i=0; i<MAX_PICTURE_COUNT; i++){
  754. if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
  755. }
  756. for(i=0; i<MAX_PICTURE_COUNT; i++){
  757. if(s->picture[i].data[0]==NULL) return i;
  758. }
  759. }
  760. av_log(s->avctx, AV_LOG_FATAL, "Internal error, picture buffer overflow\n");
  761. /* We could return -1, but the codec would crash trying to draw into a
  762. * non-existing frame anyway. This is safer than waiting for a random crash.
  763. * Also the return of this is never useful, an encoder must only allocate
  764. * as much as allowed in the specification. This has no relationship to how
  765. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  766. * enough for such valid streams).
  767. * Plus, a decoder has to check stream validity and remove frames if too
  768. * many reference frames are around. Waiting for "OOM" is not correct at
  769. * all. Similarly, missing reference frames have to be replaced by
  770. * interpolated/MC frames, anything else is a bug in the codec ...
  771. */
  772. abort();
  773. return -1;
  774. }
  775. static void update_noise_reduction(MpegEncContext *s){
  776. int intra, i;
  777. for(intra=0; intra<2; intra++){
  778. if(s->dct_count[intra] > (1<<16)){
  779. for(i=0; i<64; i++){
  780. s->dct_error_sum[intra][i] >>=1;
  781. }
  782. s->dct_count[intra] >>= 1;
  783. }
  784. for(i=0; i<64; i++){
  785. 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);
  786. }
  787. }
  788. }
  789. /**
  790. * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
  791. */
  792. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  793. {
  794. int i;
  795. Picture *pic;
  796. s->mb_skipped = 0;
  797. assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
  798. /* mark&release old frames */
  799. if (s->pict_type != FF_B_TYPE && s->last_picture_ptr && s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr->data[0]) {
  800. if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
  801. free_frame_buffer(s, s->last_picture_ptr);
  802. /* release forgotten pictures */
  803. /* if(mpeg124/h263) */
  804. if(!s->encoding){
  805. for(i=0; i<MAX_PICTURE_COUNT; i++){
  806. if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
  807. av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
  808. free_frame_buffer(s, &s->picture[i]);
  809. }
  810. }
  811. }
  812. }
  813. }
  814. if(!s->encoding){
  815. /* release non reference frames */
  816. for(i=0; i<MAX_PICTURE_COUNT; i++){
  817. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  818. free_frame_buffer(s, &s->picture[i]);
  819. }
  820. }
  821. if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
  822. pic= s->current_picture_ptr; //we already have a unused image (maybe it was set before reading the header)
  823. else{
  824. i= ff_find_unused_picture(s, 0);
  825. pic= &s->picture[i];
  826. }
  827. pic->reference= 0;
  828. if (!s->dropable){
  829. if (s->codec_id == CODEC_ID_H264)
  830. pic->reference = s->picture_structure;
  831. else if (s->pict_type != FF_B_TYPE)
  832. pic->reference = 3;
  833. }
  834. pic->coded_picture_number= s->coded_picture_number++;
  835. if(ff_alloc_picture(s, pic, 0) < 0)
  836. return -1;
  837. s->current_picture_ptr= pic;
  838. s->current_picture_ptr->top_field_first= s->top_field_first; //FIXME use only the vars from current_pic
  839. s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
  840. }
  841. s->current_picture_ptr->pict_type= s->pict_type;
  842. // if(s->flags && CODEC_FLAG_QSCALE)
  843. // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
  844. s->current_picture_ptr->key_frame= s->pict_type == FF_I_TYPE;
  845. ff_copy_picture(&s->current_picture, s->current_picture_ptr);
  846. if (s->pict_type != FF_B_TYPE) {
  847. s->last_picture_ptr= s->next_picture_ptr;
  848. if(!s->dropable)
  849. s->next_picture_ptr= s->current_picture_ptr;
  850. }
  851. /* av_log(s->avctx, AV_LOG_DEBUG, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n", s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  852. s->last_picture_ptr ? s->last_picture_ptr->data[0] : NULL,
  853. s->next_picture_ptr ? s->next_picture_ptr->data[0] : NULL,
  854. s->current_picture_ptr ? s->current_picture_ptr->data[0] : NULL,
  855. s->pict_type, s->dropable);*/
  856. if(s->codec_id != CODEC_ID_H264){
  857. if((s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL) && s->pict_type!=FF_I_TYPE){
  858. av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
  859. /* Allocate a dummy frame */
  860. i= ff_find_unused_picture(s, 0);
  861. s->last_picture_ptr= &s->picture[i];
  862. if(ff_alloc_picture(s, s->last_picture_ptr, 0) < 0)
  863. return -1;
  864. }
  865. if((s->next_picture_ptr==NULL || s->next_picture_ptr->data[0]==NULL) && s->pict_type==FF_B_TYPE){
  866. /* Allocate a dummy frame */
  867. i= ff_find_unused_picture(s, 0);
  868. s->next_picture_ptr= &s->picture[i];
  869. if(ff_alloc_picture(s, s->next_picture_ptr, 0) < 0)
  870. return -1;
  871. }
  872. }
  873. if(s->last_picture_ptr) ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  874. if(s->next_picture_ptr) ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  875. assert(s->pict_type == FF_I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
  876. if(s->picture_structure!=PICT_FRAME && s->out_format != FMT_H264){
  877. int i;
  878. for(i=0; i<4; i++){
  879. if(s->picture_structure == PICT_BOTTOM_FIELD){
  880. s->current_picture.data[i] += s->current_picture.linesize[i];
  881. }
  882. s->current_picture.linesize[i] *= 2;
  883. s->last_picture.linesize[i] *=2;
  884. s->next_picture.linesize[i] *=2;
  885. }
  886. }
  887. s->hurry_up= s->avctx->hurry_up;
  888. s->error_recognition= avctx->error_recognition;
  889. /* set dequantizer, we can't do it during init as it might change for mpeg4
  890. and we can't do it in the header decode as init is not called for mpeg4 there yet */
  891. if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
  892. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  893. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  894. }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  895. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  896. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  897. }else{
  898. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  899. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  900. }
  901. if(s->dct_error_sum){
  902. assert(s->avctx->noise_reduction && s->encoding);
  903. update_noise_reduction(s);
  904. }
  905. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  906. return ff_xvmc_field_start(s, avctx);
  907. return 0;
  908. }
  909. /* generic function for encode/decode called after a frame has been coded/decoded */
  910. void MPV_frame_end(MpegEncContext *s)
  911. {
  912. int i;
  913. /* draw edge for correct motion prediction if outside */
  914. //just to make sure that all data is rendered.
  915. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  916. ff_xvmc_field_end(s);
  917. }else if(!s->avctx->hwaccel
  918. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  919. && s->unrestricted_mv
  920. && s->current_picture.reference
  921. && !s->intra_only
  922. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  923. s->dsp.draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
  924. s->dsp.draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  925. s->dsp.draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  926. }
  927. emms_c();
  928. s->last_pict_type = s->pict_type;
  929. s->last_lambda_for[s->pict_type]= s->current_picture_ptr->quality;
  930. if(s->pict_type!=FF_B_TYPE){
  931. s->last_non_b_pict_type= s->pict_type;
  932. }
  933. #if 0
  934. /* copy back current_picture variables */
  935. for(i=0; i<MAX_PICTURE_COUNT; i++){
  936. if(s->picture[i].data[0] == s->current_picture.data[0]){
  937. s->picture[i]= s->current_picture;
  938. break;
  939. }
  940. }
  941. assert(i<MAX_PICTURE_COUNT);
  942. #endif
  943. if(s->encoding){
  944. /* release non-reference frames */
  945. for(i=0; i<MAX_PICTURE_COUNT; i++){
  946. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  947. free_frame_buffer(s, &s->picture[i]);
  948. }
  949. }
  950. }
  951. // clear copies, to avoid confusion
  952. #if 0
  953. memset(&s->last_picture, 0, sizeof(Picture));
  954. memset(&s->next_picture, 0, sizeof(Picture));
  955. memset(&s->current_picture, 0, sizeof(Picture));
  956. #endif
  957. s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
  958. }
  959. /**
  960. * draws an line from (ex, ey) -> (sx, sy).
  961. * @param w width of the image
  962. * @param h height of the image
  963. * @param stride stride/linesize of the image
  964. * @param color color of the arrow
  965. */
  966. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  967. int x, y, fr, f;
  968. sx= av_clip(sx, 0, w-1);
  969. sy= av_clip(sy, 0, h-1);
  970. ex= av_clip(ex, 0, w-1);
  971. ey= av_clip(ey, 0, h-1);
  972. buf[sy*stride + sx]+= color;
  973. if(FFABS(ex - sx) > FFABS(ey - sy)){
  974. if(sx > ex){
  975. FFSWAP(int, sx, ex);
  976. FFSWAP(int, sy, ey);
  977. }
  978. buf+= sx + sy*stride;
  979. ex-= sx;
  980. f= ((ey-sy)<<16)/ex;
  981. for(x= 0; x <= ex; x++){
  982. y = (x*f)>>16;
  983. fr= (x*f)&0xFFFF;
  984. buf[ y *stride + x]+= (color*(0x10000-fr))>>16;
  985. buf[(y+1)*stride + x]+= (color* fr )>>16;
  986. }
  987. }else{
  988. if(sy > ey){
  989. FFSWAP(int, sx, ex);
  990. FFSWAP(int, sy, ey);
  991. }
  992. buf+= sx + sy*stride;
  993. ey-= sy;
  994. if(ey) f= ((ex-sx)<<16)/ey;
  995. else f= 0;
  996. for(y= 0; y <= ey; y++){
  997. x = (y*f)>>16;
  998. fr= (y*f)&0xFFFF;
  999. buf[y*stride + x ]+= (color*(0x10000-fr))>>16;
  1000. buf[y*stride + x+1]+= (color* fr )>>16;
  1001. }
  1002. }
  1003. }
  1004. /**
  1005. * draws an arrow from (ex, ey) -> (sx, sy).
  1006. * @param w width of the image
  1007. * @param h height of the image
  1008. * @param stride stride/linesize of the image
  1009. * @param color color of the arrow
  1010. */
  1011. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1012. int dx,dy;
  1013. sx= av_clip(sx, -100, w+100);
  1014. sy= av_clip(sy, -100, h+100);
  1015. ex= av_clip(ex, -100, w+100);
  1016. ey= av_clip(ey, -100, h+100);
  1017. dx= ex - sx;
  1018. dy= ey - sy;
  1019. if(dx*dx + dy*dy > 3*3){
  1020. int rx= dx + dy;
  1021. int ry= -dx + dy;
  1022. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  1023. //FIXME subpixel accuracy
  1024. rx= ROUNDED_DIV(rx*3<<4, length);
  1025. ry= ROUNDED_DIV(ry*3<<4, length);
  1026. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1027. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1028. }
  1029. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1030. }
  1031. /**
  1032. * prints debuging info for the given picture.
  1033. */
  1034. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
  1035. if(s->avctx->hwaccel || !pict || !pict->mb_type) return;
  1036. if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
  1037. int x,y;
  1038. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1039. switch (pict->pict_type) {
  1040. case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
  1041. case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
  1042. case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
  1043. case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
  1044. case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
  1045. case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;
  1046. }
  1047. for(y=0; y<s->mb_height; y++){
  1048. for(x=0; x<s->mb_width; x++){
  1049. if(s->avctx->debug&FF_DEBUG_SKIP){
  1050. int count= s->mbskip_table[x + y*s->mb_stride];
  1051. if(count>9) count=9;
  1052. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1053. }
  1054. if(s->avctx->debug&FF_DEBUG_QP){
  1055. av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
  1056. }
  1057. if(s->avctx->debug&FF_DEBUG_MB_TYPE){
  1058. int mb_type= pict->mb_type[x + y*s->mb_stride];
  1059. //Type & MV direction
  1060. if(IS_PCM(mb_type))
  1061. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1062. else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1063. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1064. else if(IS_INTRA4x4(mb_type))
  1065. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1066. else if(IS_INTRA16x16(mb_type))
  1067. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1068. else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1069. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1070. else if(IS_DIRECT(mb_type))
  1071. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1072. else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
  1073. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1074. else if(IS_GMC(mb_type))
  1075. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1076. else if(IS_SKIP(mb_type))
  1077. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1078. else if(!USES_LIST(mb_type, 1))
  1079. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1080. else if(!USES_LIST(mb_type, 0))
  1081. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1082. else{
  1083. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1084. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1085. }
  1086. //segmentation
  1087. if(IS_8X8(mb_type))
  1088. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1089. else if(IS_16X8(mb_type))
  1090. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1091. else if(IS_8X16(mb_type))
  1092. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1093. else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
  1094. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1095. else
  1096. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1097. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
  1098. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1099. else
  1100. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1101. }
  1102. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1103. }
  1104. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1105. }
  1106. }
  1107. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  1108. const int shift= 1 + s->quarter_sample;
  1109. int mb_y;
  1110. uint8_t *ptr;
  1111. int i;
  1112. int h_chroma_shift, v_chroma_shift, block_height;
  1113. const int width = s->avctx->width;
  1114. const int height= s->avctx->height;
  1115. const int mv_sample_log2= 4 - pict->motion_subsample_log2;
  1116. const int mv_stride= (s->mb_width << mv_sample_log2) + (s->codec_id == CODEC_ID_H264 ? 0 : 1);
  1117. s->low_delay=0; //needed to see the vectors without trashing the buffers
  1118. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1119. for(i=0; i<3; i++){
  1120. memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*height:pict->linesize[i]*height >> v_chroma_shift);
  1121. pict->data[i]= s->visualization_buffer[i];
  1122. }
  1123. pict->type= FF_BUFFER_TYPE_COPY;
  1124. ptr= pict->data[0];
  1125. block_height = 16>>v_chroma_shift;
  1126. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  1127. int mb_x;
  1128. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  1129. const int mb_index= mb_x + mb_y*s->mb_stride;
  1130. if((s->avctx->debug_mv) && pict->motion_val){
  1131. int type;
  1132. for(type=0; type<3; type++){
  1133. int direction = 0;
  1134. switch (type) {
  1135. case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=FF_P_TYPE))
  1136. continue;
  1137. direction = 0;
  1138. break;
  1139. case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=FF_B_TYPE))
  1140. continue;
  1141. direction = 0;
  1142. break;
  1143. case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=FF_B_TYPE))
  1144. continue;
  1145. direction = 1;
  1146. break;
  1147. }
  1148. if(!USES_LIST(pict->mb_type[mb_index], direction))
  1149. continue;
  1150. if(IS_8X8(pict->mb_type[mb_index])){
  1151. int i;
  1152. for(i=0; i<4; i++){
  1153. int sx= mb_x*16 + 4 + 8*(i&1);
  1154. int sy= mb_y*16 + 4 + 8*(i>>1);
  1155. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1156. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1157. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1158. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1159. }
  1160. }else if(IS_16X8(pict->mb_type[mb_index])){
  1161. int i;
  1162. for(i=0; i<2; i++){
  1163. int sx=mb_x*16 + 8;
  1164. int sy=mb_y*16 + 4 + 8*i;
  1165. int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << (mv_sample_log2-1);
  1166. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1167. int my=(pict->motion_val[direction][xy][1]>>shift);
  1168. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1169. my*=2;
  1170. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1171. }
  1172. }else if(IS_8X16(pict->mb_type[mb_index])){
  1173. int i;
  1174. for(i=0; i<2; i++){
  1175. int sx=mb_x*16 + 4 + 8*i;
  1176. int sy=mb_y*16 + 8;
  1177. int xy= (mb_x*2 + i + mb_y*2*mv_stride) << (mv_sample_log2-1);
  1178. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1179. int my=(pict->motion_val[direction][xy][1]>>shift);
  1180. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1181. my*=2;
  1182. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1183. }
  1184. }else{
  1185. int sx= mb_x*16 + 8;
  1186. int sy= mb_y*16 + 8;
  1187. int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2;
  1188. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1189. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1190. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1191. }
  1192. }
  1193. }
  1194. if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
  1195. uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
  1196. int y;
  1197. for(y=0; y<block_height; y++){
  1198. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= c;
  1199. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= c;
  1200. }
  1201. }
  1202. if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
  1203. int mb_type= pict->mb_type[mb_index];
  1204. uint64_t u,v;
  1205. int y;
  1206. #define COLOR(theta, r)\
  1207. u= (int)(128 + r*cos(theta*3.141592/180));\
  1208. v= (int)(128 + r*sin(theta*3.141592/180));
  1209. u=v=128;
  1210. if(IS_PCM(mb_type)){
  1211. COLOR(120,48)
  1212. }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
  1213. COLOR(30,48)
  1214. }else if(IS_INTRA4x4(mb_type)){
  1215. COLOR(90,48)
  1216. }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
  1217. // COLOR(120,48)
  1218. }else if(IS_DIRECT(mb_type)){
  1219. COLOR(150,48)
  1220. }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
  1221. COLOR(170,48)
  1222. }else if(IS_GMC(mb_type)){
  1223. COLOR(190,48)
  1224. }else if(IS_SKIP(mb_type)){
  1225. // COLOR(180,48)
  1226. }else if(!USES_LIST(mb_type, 1)){
  1227. COLOR(240,48)
  1228. }else if(!USES_LIST(mb_type, 0)){
  1229. COLOR(0,48)
  1230. }else{
  1231. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1232. COLOR(300,48)
  1233. }
  1234. u*= 0x0101010101010101ULL;
  1235. v*= 0x0101010101010101ULL;
  1236. for(y=0; y<block_height; y++){
  1237. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= u;
  1238. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= v;
  1239. }
  1240. //segmentation
  1241. if(IS_8X8(mb_type) || IS_16X8(mb_type)){
  1242. *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1243. *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1244. }
  1245. if(IS_8X8(mb_type) || IS_8X16(mb_type)){
  1246. for(y=0; y<16; y++)
  1247. pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
  1248. }
  1249. if(IS_8X8(mb_type) && mv_sample_log2 >= 2){
  1250. int dm= 1 << (mv_sample_log2-2);
  1251. for(i=0; i<4; i++){
  1252. int sx= mb_x*16 + 8*(i&1);
  1253. int sy= mb_y*16 + 8*(i>>1);
  1254. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1255. //FIXME bidir
  1256. int32_t *mv = (int32_t*)&pict->motion_val[0][xy];
  1257. if(mv[0] != mv[dm] || mv[dm*mv_stride] != mv[dm*(mv_stride+1)])
  1258. for(y=0; y<8; y++)
  1259. pict->data[0][sx + 4 + (sy + y)*pict->linesize[0]]^= 0x80;
  1260. if(mv[0] != mv[dm*mv_stride] || mv[dm] != mv[dm*(mv_stride+1)])
  1261. *(uint64_t*)(pict->data[0] + sx + (sy + 4)*pict->linesize[0])^= 0x8080808080808080ULL;
  1262. }
  1263. }
  1264. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
  1265. // hmm
  1266. }
  1267. }
  1268. s->mbskip_table[mb_index]=0;
  1269. }
  1270. }
  1271. }
  1272. }
  1273. static inline int hpel_motion_lowres(MpegEncContext *s,
  1274. uint8_t *dest, uint8_t *src,
  1275. int field_based, int field_select,
  1276. int src_x, int src_y,
  1277. int width, int height, int stride,
  1278. int h_edge_pos, int v_edge_pos,
  1279. int w, int h, h264_chroma_mc_func *pix_op,
  1280. int motion_x, int motion_y)
  1281. {
  1282. const int lowres= s->avctx->lowres;
  1283. const int op_index= FFMIN(lowres, 2);
  1284. const int s_mask= (2<<lowres)-1;
  1285. int emu=0;
  1286. int sx, sy;
  1287. if(s->quarter_sample){
  1288. motion_x/=2;
  1289. motion_y/=2;
  1290. }
  1291. sx= motion_x & s_mask;
  1292. sy= motion_y & s_mask;
  1293. src_x += motion_x >> (lowres+1);
  1294. src_y += motion_y >> (lowres+1);
  1295. src += src_y * stride + src_x;
  1296. if( (unsigned)src_x > h_edge_pos - (!!sx) - w
  1297. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1298. ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  1299. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1300. src= s->edge_emu_buffer;
  1301. emu=1;
  1302. }
  1303. sx= (sx << 2) >> lowres;
  1304. sy= (sy << 2) >> lowres;
  1305. if(field_select)
  1306. src += s->linesize;
  1307. pix_op[op_index](dest, src, stride, h, sx, sy);
  1308. return emu;
  1309. }
  1310. /* apply one mpeg motion vector to the three components */
  1311. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1312. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1313. int field_based, int bottom_field, int field_select,
  1314. uint8_t **ref_picture, h264_chroma_mc_func *pix_op,
  1315. int motion_x, int motion_y, int h, int mb_y)
  1316. {
  1317. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1318. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy, uvsx, uvsy;
  1319. const int lowres= s->avctx->lowres;
  1320. const int op_index= FFMIN(lowres, 2);
  1321. const int block_s= 8>>lowres;
  1322. const int s_mask= (2<<lowres)-1;
  1323. const int h_edge_pos = s->h_edge_pos >> lowres;
  1324. const int v_edge_pos = s->v_edge_pos >> lowres;
  1325. linesize = s->current_picture.linesize[0] << field_based;
  1326. uvlinesize = s->current_picture.linesize[1] << field_based;
  1327. if(s->quarter_sample){ //FIXME obviously not perfect but qpel will not work in lowres anyway
  1328. motion_x/=2;
  1329. motion_y/=2;
  1330. }
  1331. if(field_based){
  1332. motion_y += (bottom_field - field_select)*((1<<lowres)-1);
  1333. }
  1334. sx= motion_x & s_mask;
  1335. sy= motion_y & s_mask;
  1336. src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1));
  1337. src_y =( mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1));
  1338. if (s->out_format == FMT_H263) {
  1339. uvsx = ((motion_x>>1) & s_mask) | (sx&1);
  1340. uvsy = ((motion_y>>1) & s_mask) | (sy&1);
  1341. uvsrc_x = src_x>>1;
  1342. uvsrc_y = src_y>>1;
  1343. }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  1344. mx = motion_x / 4;
  1345. my = motion_y / 4;
  1346. uvsx = (2*mx) & s_mask;
  1347. uvsy = (2*my) & s_mask;
  1348. uvsrc_x = s->mb_x*block_s + (mx >> lowres);
  1349. uvsrc_y = mb_y*block_s + (my >> lowres);
  1350. } else {
  1351. mx = motion_x / 2;
  1352. my = motion_y / 2;
  1353. uvsx = mx & s_mask;
  1354. uvsy = my & s_mask;
  1355. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1356. uvsrc_y =( mb_y*block_s>>field_based) + (my >> (lowres+1));
  1357. }
  1358. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1359. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1360. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1361. if( (unsigned)src_x > h_edge_pos - (!!sx) - 2*block_s
  1362. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1363. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  1364. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1365. ptr_y = s->edge_emu_buffer;
  1366. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1367. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  1368. ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
  1369. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1370. ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
  1371. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1372. ptr_cb= uvbuf;
  1373. ptr_cr= uvbuf+16;
  1374. }
  1375. }
  1376. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
  1377. dest_y += s->linesize;
  1378. dest_cb+= s->uvlinesize;
  1379. dest_cr+= s->uvlinesize;
  1380. }
  1381. if(field_select){
  1382. ptr_y += s->linesize;
  1383. ptr_cb+= s->uvlinesize;
  1384. ptr_cr+= s->uvlinesize;
  1385. }
  1386. sx= (sx << 2) >> lowres;
  1387. sy= (sy << 2) >> lowres;
  1388. pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy);
  1389. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1390. uvsx= (uvsx << 2) >> lowres;
  1391. uvsy= (uvsy << 2) >> lowres;
  1392. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1393. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1394. }
  1395. //FIXME h261 lowres loop filter
  1396. }
  1397. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1398. uint8_t *dest_cb, uint8_t *dest_cr,
  1399. uint8_t **ref_picture,
  1400. h264_chroma_mc_func *pix_op,
  1401. int mx, int my){
  1402. const int lowres= s->avctx->lowres;
  1403. const int op_index= FFMIN(lowres, 2);
  1404. const int block_s= 8>>lowres;
  1405. const int s_mask= (2<<lowres)-1;
  1406. const int h_edge_pos = s->h_edge_pos >> (lowres+1);
  1407. const int v_edge_pos = s->v_edge_pos >> (lowres+1);
  1408. int emu=0, src_x, src_y, offset, sx, sy;
  1409. uint8_t *ptr;
  1410. if(s->quarter_sample){
  1411. mx/=2;
  1412. my/=2;
  1413. }
  1414. /* In case of 8X8, we construct a single chroma motion vector
  1415. with a special rounding */
  1416. mx= ff_h263_round_chroma(mx);
  1417. my= ff_h263_round_chroma(my);
  1418. sx= mx & s_mask;
  1419. sy= my & s_mask;
  1420. src_x = s->mb_x*block_s + (mx >> (lowres+1));
  1421. src_y = s->mb_y*block_s + (my >> (lowres+1));
  1422. offset = src_y * s->uvlinesize + src_x;
  1423. ptr = ref_picture[1] + offset;
  1424. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1425. if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s
  1426. || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){
  1427. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1428. ptr= s->edge_emu_buffer;
  1429. emu=1;
  1430. }
  1431. }
  1432. sx= (sx << 2) >> lowres;
  1433. sy= (sy << 2) >> lowres;
  1434. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1435. ptr = ref_picture[2] + offset;
  1436. if(emu){
  1437. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1438. ptr= s->edge_emu_buffer;
  1439. }
  1440. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1441. }
  1442. /**
  1443. * motion compensation of a single macroblock
  1444. * @param s context
  1445. * @param dest_y luma destination pointer
  1446. * @param dest_cb chroma cb/u destination pointer
  1447. * @param dest_cr chroma cr/v destination pointer
  1448. * @param dir direction (0->forward, 1->backward)
  1449. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1450. * @param pic_op halfpel motion compensation function (average or put normally)
  1451. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1452. */
  1453. static inline void MPV_motion_lowres(MpegEncContext *s,
  1454. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1455. int dir, uint8_t **ref_picture,
  1456. h264_chroma_mc_func *pix_op)
  1457. {
  1458. int mx, my;
  1459. int mb_x, mb_y, i;
  1460. const int lowres= s->avctx->lowres;
  1461. const int block_s= 8>>lowres;
  1462. mb_x = s->mb_x;
  1463. mb_y = s->mb_y;
  1464. switch(s->mv_type) {
  1465. case MV_TYPE_16X16:
  1466. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1467. 0, 0, 0,
  1468. ref_picture, pix_op,
  1469. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s, mb_y);
  1470. break;
  1471. case MV_TYPE_8X8:
  1472. mx = 0;
  1473. my = 0;
  1474. for(i=0;i<4;i++) {
  1475. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) * s->linesize)*block_s,
  1476. ref_picture[0], 0, 0,
  1477. (2*mb_x + (i & 1))*block_s, (2*mb_y + (i >>1))*block_s,
  1478. s->width, s->height, s->linesize,
  1479. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1480. block_s, block_s, pix_op,
  1481. s->mv[dir][i][0], s->mv[dir][i][1]);
  1482. mx += s->mv[dir][i][0];
  1483. my += s->mv[dir][i][1];
  1484. }
  1485. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  1486. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my);
  1487. break;
  1488. case MV_TYPE_FIELD:
  1489. if (s->picture_structure == PICT_FRAME) {
  1490. /* top field */
  1491. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1492. 1, 0, s->field_select[dir][0],
  1493. ref_picture, pix_op,
  1494. s->mv[dir][0][0], s->mv[dir][0][1], block_s, mb_y);
  1495. /* bottom field */
  1496. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1497. 1, 1, s->field_select[dir][1],
  1498. ref_picture, pix_op,
  1499. s->mv[dir][1][0], s->mv[dir][1][1], block_s, mb_y);
  1500. } else {
  1501. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != FF_B_TYPE && !s->first_field){
  1502. ref_picture= s->current_picture_ptr->data;
  1503. }
  1504. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1505. 0, 0, s->field_select[dir][0],
  1506. ref_picture, pix_op,
  1507. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s, mb_y>>1);
  1508. }
  1509. break;
  1510. case MV_TYPE_16X8:
  1511. for(i=0; i<2; i++){
  1512. uint8_t ** ref2picture;
  1513. if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == FF_B_TYPE || s->first_field){
  1514. ref2picture= ref_picture;
  1515. }else{
  1516. ref2picture= s->current_picture_ptr->data;
  1517. }
  1518. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1519. 0, 0, s->field_select[dir][i],
  1520. ref2picture, pix_op,
  1521. s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s, mb_y>>1);
  1522. dest_y += 2*block_s*s->linesize;
  1523. dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1524. dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1525. }
  1526. break;
  1527. case MV_TYPE_DMV:
  1528. if(s->picture_structure == PICT_FRAME){
  1529. for(i=0; i<2; i++){
  1530. int j;
  1531. for(j=0; j<2; j++){
  1532. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1533. 1, j, j^i,
  1534. ref_picture, pix_op,
  1535. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], block_s, mb_y);
  1536. }
  1537. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1538. }
  1539. }else{
  1540. for(i=0; i<2; i++){
  1541. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1542. 0, 0, s->picture_structure != i+1,
  1543. ref_picture, pix_op,
  1544. s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s, mb_y>>1);
  1545. // after put we make avg of the same block
  1546. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1547. //opposite parity is always in the same frame if this is second field
  1548. if(!s->first_field){
  1549. ref_picture = s->current_picture_ptr->data;
  1550. }
  1551. }
  1552. }
  1553. break;
  1554. default: assert(0);
  1555. }
  1556. }
  1557. /* put block[] to dest[] */
  1558. static inline void put_dct(MpegEncContext *s,
  1559. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1560. {
  1561. s->dct_unquantize_intra(s, block, i, qscale);
  1562. s->dsp.idct_put (dest, line_size, block);
  1563. }
  1564. /* add block[] to dest[] */
  1565. static inline void add_dct(MpegEncContext *s,
  1566. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1567. {
  1568. if (s->block_last_index[i] >= 0) {
  1569. s->dsp.idct_add (dest, line_size, block);
  1570. }
  1571. }
  1572. static inline void add_dequant_dct(MpegEncContext *s,
  1573. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1574. {
  1575. if (s->block_last_index[i] >= 0) {
  1576. s->dct_unquantize_inter(s, block, i, qscale);
  1577. s->dsp.idct_add (dest, line_size, block);
  1578. }
  1579. }
  1580. /**
  1581. * cleans dc, ac, coded_block for the current non intra MB
  1582. */
  1583. void ff_clean_intra_table_entries(MpegEncContext *s)
  1584. {
  1585. int wrap = s->b8_stride;
  1586. int xy = s->block_index[0];
  1587. s->dc_val[0][xy ] =
  1588. s->dc_val[0][xy + 1 ] =
  1589. s->dc_val[0][xy + wrap] =
  1590. s->dc_val[0][xy + 1 + wrap] = 1024;
  1591. /* ac pred */
  1592. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1593. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1594. if (s->msmpeg4_version>=3) {
  1595. s->coded_block[xy ] =
  1596. s->coded_block[xy + 1 ] =
  1597. s->coded_block[xy + wrap] =
  1598. s->coded_block[xy + 1 + wrap] = 0;
  1599. }
  1600. /* chroma */
  1601. wrap = s->mb_stride;
  1602. xy = s->mb_x + s->mb_y * wrap;
  1603. s->dc_val[1][xy] =
  1604. s->dc_val[2][xy] = 1024;
  1605. /* ac pred */
  1606. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1607. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1608. s->mbintra_table[xy]= 0;
  1609. }
  1610. /* generic function called after a macroblock has been parsed by the
  1611. decoder or after it has been encoded by the encoder.
  1612. Important variables used:
  1613. s->mb_intra : true if intra macroblock
  1614. s->mv_dir : motion vector direction
  1615. s->mv_type : motion vector type
  1616. s->mv : motion vector
  1617. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1618. */
  1619. static av_always_inline
  1620. void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64],
  1621. int lowres_flag, int is_mpeg12)
  1622. {
  1623. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1624. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1625. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1626. return;
  1627. }
  1628. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1629. /* save DCT coefficients */
  1630. int i,j;
  1631. DCTELEM *dct = &s->current_picture.dct_coeff[mb_xy*64*6];
  1632. for(i=0; i<6; i++)
  1633. for(j=0; j<64; j++)
  1634. *dct++ = block[i][s->dsp.idct_permutation[j]];
  1635. }
  1636. s->current_picture.qscale_table[mb_xy]= s->qscale;
  1637. /* update DC predictors for P macroblocks */
  1638. if (!s->mb_intra) {
  1639. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1640. if(s->mbintra_table[mb_xy])
  1641. ff_clean_intra_table_entries(s);
  1642. } else {
  1643. s->last_dc[0] =
  1644. s->last_dc[1] =
  1645. s->last_dc[2] = 128 << s->intra_dc_precision;
  1646. }
  1647. }
  1648. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1649. s->mbintra_table[mb_xy]=1;
  1650. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==FF_B_TYPE) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
  1651. uint8_t *dest_y, *dest_cb, *dest_cr;
  1652. int dct_linesize, dct_offset;
  1653. op_pixels_func (*op_pix)[4];
  1654. qpel_mc_func (*op_qpix)[16];
  1655. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
  1656. const int uvlinesize= s->current_picture.linesize[1];
  1657. const int readable= s->pict_type != FF_B_TYPE || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  1658. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  1659. /* avoid copy if macroblock skipped in last frame too */
  1660. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1661. if(!s->encoding){
  1662. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1663. const int age= s->current_picture.age;
  1664. assert(age);
  1665. if (s->mb_skipped) {
  1666. s->mb_skipped= 0;
  1667. assert(s->pict_type!=FF_I_TYPE);
  1668. (*mbskip_ptr) ++; /* indicate that this time we skipped it */
  1669. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1670. /* if previous was skipped too, then nothing to do ! */
  1671. if (*mbskip_ptr >= age && s->current_picture.reference){
  1672. return;
  1673. }
  1674. } else if(!s->current_picture.reference){
  1675. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  1676. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1677. } else{
  1678. *mbskip_ptr = 0; /* not skipped */
  1679. }
  1680. }
  1681. dct_linesize = linesize << s->interlaced_dct;
  1682. dct_offset =(s->interlaced_dct)? linesize : linesize*block_size;
  1683. if(readable){
  1684. dest_y= s->dest[0];
  1685. dest_cb= s->dest[1];
  1686. dest_cr= s->dest[2];
  1687. }else{
  1688. dest_y = s->b_scratchpad;
  1689. dest_cb= s->b_scratchpad+16*linesize;
  1690. dest_cr= s->b_scratchpad+32*linesize;
  1691. }
  1692. if (!s->mb_intra) {
  1693. /* motion handling */
  1694. /* decoding or more than one mb_type (MC was already done otherwise) */
  1695. if(!s->encoding){
  1696. if(lowres_flag){
  1697. h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
  1698. if (s->mv_dir & MV_DIR_FORWARD) {
  1699. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix);
  1700. op_pix = s->dsp.avg_h264_chroma_pixels_tab;
  1701. }
  1702. if (s->mv_dir & MV_DIR_BACKWARD) {
  1703. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix);
  1704. }
  1705. }else{
  1706. op_qpix= s->me.qpel_put;
  1707. if ((!s->no_rounding) || s->pict_type==FF_B_TYPE){
  1708. op_pix = s->dsp.put_pixels_tab;
  1709. }else{
  1710. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1711. }
  1712. if (s->mv_dir & MV_DIR_FORWARD) {
  1713. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  1714. op_pix = s->dsp.avg_pixels_tab;
  1715. op_qpix= s->me.qpel_avg;
  1716. }
  1717. if (s->mv_dir & MV_DIR_BACKWARD) {
  1718. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  1719. }
  1720. }
  1721. }
  1722. /* skip dequant / idct if we are really late ;) */
  1723. if(s->hurry_up>1) goto skip_idct;
  1724. if(s->avctx->skip_idct){
  1725. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == FF_B_TYPE)
  1726. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != FF_I_TYPE)
  1727. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1728. goto skip_idct;
  1729. }
  1730. /* add dct residue */
  1731. if(s->encoding || !( s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  1732. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1733. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1734. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1735. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1736. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1737. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1738. if (s->chroma_y_shift){
  1739. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1740. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1741. }else{
  1742. dct_linesize >>= 1;
  1743. dct_offset >>=1;
  1744. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1745. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1746. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1747. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1748. }
  1749. }
  1750. } else if(is_mpeg12 || (s->codec_id != CODEC_ID_WMV2)){
  1751. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1752. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1753. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1754. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1755. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1756. if(s->chroma_y_shift){//Chroma420
  1757. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1758. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1759. }else{
  1760. //chroma422
  1761. dct_linesize = uvlinesize << s->interlaced_dct;
  1762. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  1763. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1764. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1765. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1766. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1767. if(!s->chroma_x_shift){//Chroma444
  1768. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  1769. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  1770. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  1771. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  1772. }
  1773. }
  1774. }//fi gray
  1775. }
  1776. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1777. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1778. }
  1779. } else {
  1780. /* dct only in intra block */
  1781. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  1782. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1783. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1784. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1785. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1786. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1787. if(s->chroma_y_shift){
  1788. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1789. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1790. }else{
  1791. dct_offset >>=1;
  1792. dct_linesize >>=1;
  1793. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1794. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1795. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1796. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1797. }
  1798. }
  1799. }else{
  1800. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1801. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1802. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1803. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1804. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1805. if(s->chroma_y_shift){
  1806. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1807. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1808. }else{
  1809. dct_linesize = uvlinesize << s->interlaced_dct;
  1810. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
  1811. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  1812. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  1813. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1814. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1815. if(!s->chroma_x_shift){//Chroma444
  1816. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  1817. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  1818. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  1819. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  1820. }
  1821. }
  1822. }//gray
  1823. }
  1824. }
  1825. skip_idct:
  1826. if(!readable){
  1827. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1828. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1829. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1830. }
  1831. }
  1832. }
  1833. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
  1834. #if !CONFIG_SMALL
  1835. if(s->out_format == FMT_MPEG1) {
  1836. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  1837. else MPV_decode_mb_internal(s, block, 0, 1);
  1838. } else
  1839. #endif
  1840. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  1841. else MPV_decode_mb_internal(s, block, 0, 0);
  1842. }
  1843. /**
  1844. *
  1845. * @param h is the normal height, this will be reduced automatically if needed for the last row
  1846. */
  1847. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  1848. if (s->avctx->draw_horiz_band) {
  1849. AVFrame *src;
  1850. const int field_pic= s->picture_structure != PICT_FRAME;
  1851. int offset[4];
  1852. h= FFMIN(h, (s->avctx->height>>field_pic) - y);
  1853. if(field_pic && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)){
  1854. h <<= 1;
  1855. y <<= 1;
  1856. if(s->first_field) return;
  1857. }
  1858. if(s->pict_type==FF_B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  1859. src= (AVFrame*)s->current_picture_ptr;
  1860. else if(s->last_picture_ptr)
  1861. src= (AVFrame*)s->last_picture_ptr;
  1862. else
  1863. return;
  1864. if(s->pict_type==FF_B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  1865. offset[0]=
  1866. offset[1]=
  1867. offset[2]=
  1868. offset[3]= 0;
  1869. }else{
  1870. offset[0]= y * s->linesize;
  1871. offset[1]=
  1872. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  1873. offset[3]= 0;
  1874. }
  1875. emms_c();
  1876. s->avctx->draw_horiz_band(s->avctx, src, offset,
  1877. y, s->picture_structure, h);
  1878. }
  1879. }
  1880. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  1881. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
  1882. const int uvlinesize= s->current_picture.linesize[1];
  1883. const int mb_size= 4 - s->avctx->lowres;
  1884. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  1885. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  1886. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  1887. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  1888. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  1889. s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  1890. //block_index is not used by mpeg2, so it is not affected by chroma_format
  1891. s->dest[0] = s->current_picture.data[0] + ((s->mb_x - 1) << mb_size);
  1892. s->dest[1] = s->current_picture.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  1893. s->dest[2] = s->current_picture.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  1894. if(!(s->pict_type==FF_B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  1895. {
  1896. if(s->picture_structure==PICT_FRAME){
  1897. s->dest[0] += s->mb_y * linesize << mb_size;
  1898. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1899. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1900. }else{
  1901. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  1902. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  1903. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  1904. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  1905. }
  1906. }
  1907. }
  1908. void ff_mpeg_flush(AVCodecContext *avctx){
  1909. int i;
  1910. MpegEncContext *s = avctx->priv_data;
  1911. if(s==NULL || s->picture==NULL)
  1912. return;
  1913. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1914. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  1915. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  1916. free_frame_buffer(s, &s->picture[i]);
  1917. }
  1918. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  1919. s->mb_x= s->mb_y= 0;
  1920. s->closed_gop= 0;
  1921. s->parse_context.state= -1;
  1922. s->parse_context.frame_start_found= 0;
  1923. s->parse_context.overread= 0;
  1924. s->parse_context.overread_index= 0;
  1925. s->parse_context.index= 0;
  1926. s->parse_context.last_index= 0;
  1927. s->bitstream_buffer_size=0;
  1928. s->pp_time=0;
  1929. }
  1930. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  1931. DCTELEM *block, int n, int qscale)
  1932. {
  1933. int i, level, nCoeffs;
  1934. const uint16_t *quant_matrix;
  1935. nCoeffs= s->block_last_index[n];
  1936. if (n < 4)
  1937. block[0] = block[0] * s->y_dc_scale;
  1938. else
  1939. block[0] = block[0] * s->c_dc_scale;
  1940. /* XXX: only mpeg1 */
  1941. quant_matrix = s->intra_matrix;
  1942. for(i=1;i<=nCoeffs;i++) {
  1943. int j= s->intra_scantable.permutated[i];
  1944. level = block[j];
  1945. if (level) {
  1946. if (level < 0) {
  1947. level = -level;
  1948. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1949. level = (level - 1) | 1;
  1950. level = -level;
  1951. } else {
  1952. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  1953. level = (level - 1) | 1;
  1954. }
  1955. block[j] = level;
  1956. }
  1957. }
  1958. }
  1959. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  1960. DCTELEM *block, int n, int qscale)
  1961. {
  1962. int i, level, nCoeffs;
  1963. const uint16_t *quant_matrix;
  1964. nCoeffs= s->block_last_index[n];
  1965. quant_matrix = s->inter_matrix;
  1966. for(i=0; i<=nCoeffs; i++) {
  1967. int j= s->intra_scantable.permutated[i];
  1968. level = block[j];
  1969. if (level) {
  1970. if (level < 0) {
  1971. level = -level;
  1972. level = (((level << 1) + 1) * qscale *
  1973. ((int) (quant_matrix[j]))) >> 4;
  1974. level = (level - 1) | 1;
  1975. level = -level;
  1976. } else {
  1977. level = (((level << 1) + 1) * qscale *
  1978. ((int) (quant_matrix[j]))) >> 4;
  1979. level = (level - 1) | 1;
  1980. }
  1981. block[j] = level;
  1982. }
  1983. }
  1984. }
  1985. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  1986. DCTELEM *block, int n, int qscale)
  1987. {
  1988. int i, level, nCoeffs;
  1989. const uint16_t *quant_matrix;
  1990. if(s->alternate_scan) nCoeffs= 63;
  1991. else nCoeffs= s->block_last_index[n];
  1992. if (n < 4)
  1993. block[0] = block[0] * s->y_dc_scale;
  1994. else
  1995. block[0] = block[0] * s->c_dc_scale;
  1996. quant_matrix = s->intra_matrix;
  1997. for(i=1;i<=nCoeffs;i++) {
  1998. int j= s->intra_scantable.permutated[i];
  1999. level = block[j];
  2000. if (level) {
  2001. if (level < 0) {
  2002. level = -level;
  2003. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2004. level = -level;
  2005. } else {
  2006. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2007. }
  2008. block[j] = level;
  2009. }
  2010. }
  2011. }
  2012. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2013. DCTELEM *block, int n, int qscale)
  2014. {
  2015. int i, level, nCoeffs;
  2016. const uint16_t *quant_matrix;
  2017. int sum=-1;
  2018. if(s->alternate_scan) nCoeffs= 63;
  2019. else nCoeffs= s->block_last_index[n];
  2020. if (n < 4)
  2021. block[0] = block[0] * s->y_dc_scale;
  2022. else
  2023. block[0] = block[0] * s->c_dc_scale;
  2024. quant_matrix = s->intra_matrix;
  2025. for(i=1;i<=nCoeffs;i++) {
  2026. int j= s->intra_scantable.permutated[i];
  2027. level = block[j];
  2028. if (level) {
  2029. if (level < 0) {
  2030. level = -level;
  2031. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2032. level = -level;
  2033. } else {
  2034. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2035. }
  2036. block[j] = level;
  2037. sum+=level;
  2038. }
  2039. }
  2040. block[63]^=sum&1;
  2041. }
  2042. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2043. DCTELEM *block, int n, int qscale)
  2044. {
  2045. int i, level, nCoeffs;
  2046. const uint16_t *quant_matrix;
  2047. int sum=-1;
  2048. if(s->alternate_scan) nCoeffs= 63;
  2049. else nCoeffs= s->block_last_index[n];
  2050. quant_matrix = s->inter_matrix;
  2051. for(i=0; i<=nCoeffs; i++) {
  2052. int j= s->intra_scantable.permutated[i];
  2053. level = block[j];
  2054. if (level) {
  2055. if (level < 0) {
  2056. level = -level;
  2057. level = (((level << 1) + 1) * qscale *
  2058. ((int) (quant_matrix[j]))) >> 4;
  2059. level = -level;
  2060. } else {
  2061. level = (((level << 1) + 1) * qscale *
  2062. ((int) (quant_matrix[j]))) >> 4;
  2063. }
  2064. block[j] = level;
  2065. sum+=level;
  2066. }
  2067. }
  2068. block[63]^=sum&1;
  2069. }
  2070. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2071. DCTELEM *block, int n, int qscale)
  2072. {
  2073. int i, level, qmul, qadd;
  2074. int nCoeffs;
  2075. assert(s->block_last_index[n]>=0);
  2076. qmul = qscale << 1;
  2077. if (!s->h263_aic) {
  2078. if (n < 4)
  2079. block[0] = block[0] * s->y_dc_scale;
  2080. else
  2081. block[0] = block[0] * s->c_dc_scale;
  2082. qadd = (qscale - 1) | 1;
  2083. }else{
  2084. qadd = 0;
  2085. }
  2086. if(s->ac_pred)
  2087. nCoeffs=63;
  2088. else
  2089. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2090. for(i=1; i<=nCoeffs; i++) {
  2091. level = block[i];
  2092. if (level) {
  2093. if (level < 0) {
  2094. level = level * qmul - qadd;
  2095. } else {
  2096. level = level * qmul + qadd;
  2097. }
  2098. block[i] = level;
  2099. }
  2100. }
  2101. }
  2102. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2103. DCTELEM *block, int n, int qscale)
  2104. {
  2105. int i, level, qmul, qadd;
  2106. int nCoeffs;
  2107. assert(s->block_last_index[n]>=0);
  2108. qadd = (qscale - 1) | 1;
  2109. qmul = qscale << 1;
  2110. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2111. for(i=0; i<=nCoeffs; i++) {
  2112. level = block[i];
  2113. if (level) {
  2114. if (level < 0) {
  2115. level = level * qmul - qadd;
  2116. } else {
  2117. level = level * qmul + qadd;
  2118. }
  2119. block[i] = level;
  2120. }
  2121. }
  2122. }
  2123. /**
  2124. * set qscale and update qscale dependent variables.
  2125. */
  2126. void ff_set_qscale(MpegEncContext * s, int qscale)
  2127. {
  2128. if (qscale < 1)
  2129. qscale = 1;
  2130. else if (qscale > 31)
  2131. qscale = 31;
  2132. s->qscale = qscale;
  2133. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2134. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2135. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2136. }