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.

3193 lines
115KB

  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/attributes.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/internal.h"
  32. #include "avcodec.h"
  33. #include "dsputil.h"
  34. #include "h264chroma.h"
  35. #include "internal.h"
  36. #include "mathops.h"
  37. #include "mpegvideo.h"
  38. #include "mjpegenc.h"
  39. #include "msmpeg4.h"
  40. #include "thread.h"
  41. #include <limits.h>
  42. static const uint8_t ff_default_chroma_qscale_table[32] = {
  43. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  44. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  45. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
  46. };
  47. const uint8_t ff_mpeg1_dc_scale_table[128] = {
  48. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  49. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  50. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  51. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  52. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  53. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  54. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  55. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  56. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  57. };
  58. static const uint8_t mpeg2_dc_scale_table1[128] = {
  59. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  60. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  61. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  62. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  63. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  64. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  65. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  66. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  67. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  68. };
  69. static const uint8_t mpeg2_dc_scale_table2[128] = {
  70. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  71. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  72. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  73. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  74. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  75. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  76. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  77. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  78. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  79. };
  80. static const uint8_t mpeg2_dc_scale_table3[128] = {
  81. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  82. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  83. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  84. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  85. 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,
  87. 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,
  89. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  90. };
  91. const uint8_t *const ff_mpeg2_dc_scale_table[4] = {
  92. ff_mpeg1_dc_scale_table,
  93. mpeg2_dc_scale_table1,
  94. mpeg2_dc_scale_table2,
  95. mpeg2_dc_scale_table3,
  96. };
  97. const enum AVPixelFormat ff_pixfmt_list_420[] = {
  98. AV_PIX_FMT_YUV420P,
  99. AV_PIX_FMT_NONE
  100. };
  101. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  102. int16_t *block, int n, int qscale)
  103. {
  104. int i, level, nCoeffs;
  105. const uint16_t *quant_matrix;
  106. nCoeffs= s->block_last_index[n];
  107. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  108. /* XXX: only mpeg1 */
  109. quant_matrix = s->intra_matrix;
  110. for(i=1;i<=nCoeffs;i++) {
  111. int j= s->intra_scantable.permutated[i];
  112. level = block[j];
  113. if (level) {
  114. if (level < 0) {
  115. level = -level;
  116. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  117. level = (level - 1) | 1;
  118. level = -level;
  119. } else {
  120. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  121. level = (level - 1) | 1;
  122. }
  123. block[j] = level;
  124. }
  125. }
  126. }
  127. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  128. int16_t *block, int n, int qscale)
  129. {
  130. int i, level, nCoeffs;
  131. const uint16_t *quant_matrix;
  132. nCoeffs= s->block_last_index[n];
  133. quant_matrix = s->inter_matrix;
  134. for(i=0; i<=nCoeffs; i++) {
  135. int j= s->intra_scantable.permutated[i];
  136. level = block[j];
  137. if (level) {
  138. if (level < 0) {
  139. level = -level;
  140. level = (((level << 1) + 1) * qscale *
  141. ((int) (quant_matrix[j]))) >> 4;
  142. level = (level - 1) | 1;
  143. level = -level;
  144. } else {
  145. level = (((level << 1) + 1) * qscale *
  146. ((int) (quant_matrix[j]))) >> 4;
  147. level = (level - 1) | 1;
  148. }
  149. block[j] = level;
  150. }
  151. }
  152. }
  153. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  154. int16_t *block, int n, int qscale)
  155. {
  156. int i, level, nCoeffs;
  157. const uint16_t *quant_matrix;
  158. if(s->alternate_scan) nCoeffs= 63;
  159. else nCoeffs= s->block_last_index[n];
  160. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  161. quant_matrix = s->intra_matrix;
  162. for(i=1;i<=nCoeffs;i++) {
  163. int j= s->intra_scantable.permutated[i];
  164. level = block[j];
  165. if (level) {
  166. if (level < 0) {
  167. level = -level;
  168. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  169. level = -level;
  170. } else {
  171. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  172. }
  173. block[j] = level;
  174. }
  175. }
  176. }
  177. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  178. int16_t *block, int n, int qscale)
  179. {
  180. int i, level, nCoeffs;
  181. const uint16_t *quant_matrix;
  182. int sum=-1;
  183. if(s->alternate_scan) nCoeffs= 63;
  184. else nCoeffs= s->block_last_index[n];
  185. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  186. sum += block[0];
  187. quant_matrix = s->intra_matrix;
  188. for(i=1;i<=nCoeffs;i++) {
  189. int j= s->intra_scantable.permutated[i];
  190. level = block[j];
  191. if (level) {
  192. if (level < 0) {
  193. level = -level;
  194. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  195. level = -level;
  196. } else {
  197. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  198. }
  199. block[j] = level;
  200. sum+=level;
  201. }
  202. }
  203. block[63]^=sum&1;
  204. }
  205. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  206. int16_t *block, int n, int qscale)
  207. {
  208. int i, level, nCoeffs;
  209. const uint16_t *quant_matrix;
  210. int sum=-1;
  211. if(s->alternate_scan) nCoeffs= 63;
  212. else nCoeffs= s->block_last_index[n];
  213. quant_matrix = s->inter_matrix;
  214. for(i=0; i<=nCoeffs; i++) {
  215. int j= s->intra_scantable.permutated[i];
  216. level = block[j];
  217. if (level) {
  218. if (level < 0) {
  219. level = -level;
  220. level = (((level << 1) + 1) * qscale *
  221. ((int) (quant_matrix[j]))) >> 4;
  222. level = -level;
  223. } else {
  224. level = (((level << 1) + 1) * qscale *
  225. ((int) (quant_matrix[j]))) >> 4;
  226. }
  227. block[j] = level;
  228. sum+=level;
  229. }
  230. }
  231. block[63]^=sum&1;
  232. }
  233. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  234. int16_t *block, int n, int qscale)
  235. {
  236. int i, level, qmul, qadd;
  237. int nCoeffs;
  238. av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
  239. qmul = qscale << 1;
  240. if (!s->h263_aic) {
  241. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  242. qadd = (qscale - 1) | 1;
  243. }else{
  244. qadd = 0;
  245. }
  246. if(s->ac_pred)
  247. nCoeffs=63;
  248. else
  249. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  250. for(i=1; i<=nCoeffs; i++) {
  251. level = block[i];
  252. if (level) {
  253. if (level < 0) {
  254. level = level * qmul - qadd;
  255. } else {
  256. level = level * qmul + qadd;
  257. }
  258. block[i] = level;
  259. }
  260. }
  261. }
  262. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  263. int16_t *block, int n, int qscale)
  264. {
  265. int i, level, qmul, qadd;
  266. int nCoeffs;
  267. av_assert2(s->block_last_index[n]>=0);
  268. qadd = (qscale - 1) | 1;
  269. qmul = qscale << 1;
  270. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  271. for(i=0; i<=nCoeffs; i++) {
  272. level = block[i];
  273. if (level) {
  274. if (level < 0) {
  275. level = level * qmul - qadd;
  276. } else {
  277. level = level * qmul + qadd;
  278. }
  279. block[i] = level;
  280. }
  281. }
  282. }
  283. static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
  284. int (*mv)[2][4][2],
  285. int mb_x, int mb_y, int mb_intra, int mb_skipped)
  286. {
  287. MpegEncContext *s = opaque;
  288. s->mv_dir = mv_dir;
  289. s->mv_type = mv_type;
  290. s->mb_intra = mb_intra;
  291. s->mb_skipped = mb_skipped;
  292. s->mb_x = mb_x;
  293. s->mb_y = mb_y;
  294. memcpy(s->mv, mv, sizeof(*mv));
  295. ff_init_block_index(s);
  296. ff_update_block_index(s);
  297. s->dsp.clear_blocks(s->block[0]);
  298. s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
  299. s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
  300. s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
  301. if (ref)
  302. av_log(s->avctx, AV_LOG_DEBUG, "Interlaced error concealment is not fully implemented\n");
  303. ff_MPV_decode_mb(s, s->block);
  304. }
  305. /* init common dct for both encoder and decoder */
  306. av_cold int ff_dct_common_init(MpegEncContext *s)
  307. {
  308. ff_dsputil_init(&s->dsp, s->avctx);
  309. ff_h264chroma_init(&s->h264chroma, 8); //for lowres
  310. ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
  311. ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
  312. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  313. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  314. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  315. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  316. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  317. if (s->flags & CODEC_FLAG_BITEXACT)
  318. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  319. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  320. if (ARCH_ALPHA)
  321. ff_MPV_common_init_axp(s);
  322. if (ARCH_ARM)
  323. ff_MPV_common_init_arm(s);
  324. if (ARCH_PPC)
  325. ff_MPV_common_init_ppc(s);
  326. if (ARCH_X86)
  327. ff_MPV_common_init_x86(s);
  328. /* load & permutate scantables
  329. * note: only wmv uses different ones
  330. */
  331. if (s->alternate_scan) {
  332. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  333. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  334. } else {
  335. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  336. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  337. }
  338. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  339. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  340. return 0;
  341. }
  342. static int frame_size_alloc(MpegEncContext *s, int linesize)
  343. {
  344. int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
  345. // edge emu needs blocksize + filter length - 1
  346. // (= 17x17 for halfpel / 21x21 for h264)
  347. // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
  348. // at uvlinesize. It supports only YUV420 so 24x24 is enough
  349. // linesize * interlaced * MBsize
  350. FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 4 * 24,
  351. fail);
  352. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 4 * 16 * 2,
  353. fail)
  354. s->me.temp = s->me.scratchpad;
  355. s->rd_scratchpad = s->me.scratchpad;
  356. s->b_scratchpad = s->me.scratchpad;
  357. s->obmc_scratchpad = s->me.scratchpad + 16;
  358. return 0;
  359. fail:
  360. av_freep(&s->edge_emu_buffer);
  361. return AVERROR(ENOMEM);
  362. }
  363. /**
  364. * Allocate a frame buffer
  365. */
  366. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  367. {
  368. int r, ret;
  369. pic->tf.f = &pic->f;
  370. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  371. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  372. s->codec_id != AV_CODEC_ID_MSS2)
  373. r = ff_thread_get_buffer(s->avctx, &pic->tf,
  374. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  375. else {
  376. pic->f.width = s->avctx->width;
  377. pic->f.height = s->avctx->height;
  378. pic->f.format = s->avctx->pix_fmt;
  379. r = avcodec_default_get_buffer2(s->avctx, &pic->f, 0);
  380. }
  381. if (r < 0 || !pic->f.buf[0]) {
  382. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
  383. r, pic->f.data[0]);
  384. return -1;
  385. }
  386. if (s->avctx->hwaccel) {
  387. assert(!pic->hwaccel_picture_private);
  388. if (s->avctx->hwaccel->priv_data_size) {
  389. pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size);
  390. if (!pic->hwaccel_priv_buf) {
  391. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  392. return -1;
  393. }
  394. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  395. }
  396. }
  397. if (s->linesize && (s->linesize != pic->f.linesize[0] ||
  398. s->uvlinesize != pic->f.linesize[1])) {
  399. av_log(s->avctx, AV_LOG_ERROR,
  400. "get_buffer() failed (stride changed)\n");
  401. ff_mpeg_unref_picture(s, pic);
  402. return -1;
  403. }
  404. if (pic->f.linesize[1] != pic->f.linesize[2]) {
  405. av_log(s->avctx, AV_LOG_ERROR,
  406. "get_buffer() failed (uv stride mismatch)\n");
  407. ff_mpeg_unref_picture(s, pic);
  408. return -1;
  409. }
  410. if (!s->edge_emu_buffer &&
  411. (ret = frame_size_alloc(s, pic->f.linesize[0])) < 0) {
  412. av_log(s->avctx, AV_LOG_ERROR,
  413. "get_buffer() failed to allocate context scratch buffers.\n");
  414. ff_mpeg_unref_picture(s, pic);
  415. return ret;
  416. }
  417. return 0;
  418. }
  419. void ff_free_picture_tables(Picture *pic)
  420. {
  421. int i;
  422. pic->alloc_mb_width =
  423. pic->alloc_mb_height = 0;
  424. av_buffer_unref(&pic->mb_var_buf);
  425. av_buffer_unref(&pic->mc_mb_var_buf);
  426. av_buffer_unref(&pic->mb_mean_buf);
  427. av_buffer_unref(&pic->mbskip_table_buf);
  428. av_buffer_unref(&pic->qscale_table_buf);
  429. av_buffer_unref(&pic->mb_type_buf);
  430. for (i = 0; i < 2; i++) {
  431. av_buffer_unref(&pic->motion_val_buf[i]);
  432. av_buffer_unref(&pic->ref_index_buf[i]);
  433. }
  434. }
  435. static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
  436. {
  437. const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
  438. const int mb_array_size = s->mb_stride * s->mb_height;
  439. const int b8_array_size = s->b8_stride * s->mb_height * 2;
  440. int i;
  441. pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
  442. pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
  443. pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
  444. sizeof(uint32_t));
  445. if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
  446. return AVERROR(ENOMEM);
  447. if (s->encoding) {
  448. pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  449. pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  450. pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
  451. if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
  452. return AVERROR(ENOMEM);
  453. }
  454. if (s->out_format == FMT_H263 || s->encoding || s->avctx->debug_mv) {
  455. int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
  456. int ref_index_size = 4 * mb_array_size;
  457. for (i = 0; mv_size && i < 2; i++) {
  458. pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
  459. pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
  460. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  461. return AVERROR(ENOMEM);
  462. }
  463. }
  464. pic->alloc_mb_width = s->mb_width;
  465. pic->alloc_mb_height = s->mb_height;
  466. return 0;
  467. }
  468. static int make_tables_writable(Picture *pic)
  469. {
  470. int ret, i;
  471. #define MAKE_WRITABLE(table) \
  472. do {\
  473. if (pic->table &&\
  474. (ret = av_buffer_make_writable(&pic->table)) < 0)\
  475. return ret;\
  476. } while (0)
  477. MAKE_WRITABLE(mb_var_buf);
  478. MAKE_WRITABLE(mc_mb_var_buf);
  479. MAKE_WRITABLE(mb_mean_buf);
  480. MAKE_WRITABLE(mbskip_table_buf);
  481. MAKE_WRITABLE(qscale_table_buf);
  482. MAKE_WRITABLE(mb_type_buf);
  483. for (i = 0; i < 2; i++) {
  484. MAKE_WRITABLE(motion_val_buf[i]);
  485. MAKE_WRITABLE(ref_index_buf[i]);
  486. }
  487. return 0;
  488. }
  489. /**
  490. * Allocate a Picture.
  491. * The pixels are allocated/set by calling get_buffer() if shared = 0
  492. */
  493. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  494. {
  495. int i, ret;
  496. if (pic->qscale_table_buf)
  497. if ( pic->alloc_mb_width != s->mb_width
  498. || pic->alloc_mb_height != s->mb_height)
  499. ff_free_picture_tables(pic);
  500. if (shared) {
  501. av_assert0(pic->f.data[0]);
  502. pic->shared = 1;
  503. } else {
  504. av_assert0(!pic->f.buf[0]);
  505. if (alloc_frame_buffer(s, pic) < 0)
  506. return -1;
  507. s->linesize = pic->f.linesize[0];
  508. s->uvlinesize = pic->f.linesize[1];
  509. }
  510. if (!pic->qscale_table_buf)
  511. ret = alloc_picture_tables(s, pic);
  512. else
  513. ret = make_tables_writable(pic);
  514. if (ret < 0)
  515. goto fail;
  516. if (s->encoding) {
  517. pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
  518. pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
  519. pic->mb_mean = pic->mb_mean_buf->data;
  520. }
  521. pic->mbskip_table = pic->mbskip_table_buf->data;
  522. pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
  523. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
  524. if (pic->motion_val_buf[0]) {
  525. for (i = 0; i < 2; i++) {
  526. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  527. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  528. }
  529. }
  530. return 0;
  531. fail:
  532. av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
  533. ff_mpeg_unref_picture(s, pic);
  534. ff_free_picture_tables(pic);
  535. return AVERROR(ENOMEM);
  536. }
  537. /**
  538. * Deallocate a picture.
  539. */
  540. void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
  541. {
  542. int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
  543. pic->tf.f = &pic->f;
  544. /* WM Image / Screen codecs allocate internal buffers with different
  545. * dimensions / colorspaces; ignore user-defined callbacks for these. */
  546. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  547. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  548. s->codec_id != AV_CODEC_ID_MSS2)
  549. ff_thread_release_buffer(s->avctx, &pic->tf);
  550. else
  551. av_frame_unref(&pic->f);
  552. av_buffer_unref(&pic->hwaccel_priv_buf);
  553. if (pic->needs_realloc)
  554. ff_free_picture_tables(pic);
  555. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  556. }
  557. static int update_picture_tables(Picture *dst, Picture *src)
  558. {
  559. int i;
  560. #define UPDATE_TABLE(table)\
  561. do {\
  562. if (src->table &&\
  563. (!dst->table || dst->table->buffer != src->table->buffer)) {\
  564. av_buffer_unref(&dst->table);\
  565. dst->table = av_buffer_ref(src->table);\
  566. if (!dst->table) {\
  567. ff_free_picture_tables(dst);\
  568. return AVERROR(ENOMEM);\
  569. }\
  570. }\
  571. } while (0)
  572. UPDATE_TABLE(mb_var_buf);
  573. UPDATE_TABLE(mc_mb_var_buf);
  574. UPDATE_TABLE(mb_mean_buf);
  575. UPDATE_TABLE(mbskip_table_buf);
  576. UPDATE_TABLE(qscale_table_buf);
  577. UPDATE_TABLE(mb_type_buf);
  578. for (i = 0; i < 2; i++) {
  579. UPDATE_TABLE(motion_val_buf[i]);
  580. UPDATE_TABLE(ref_index_buf[i]);
  581. }
  582. dst->mb_var = src->mb_var;
  583. dst->mc_mb_var = src->mc_mb_var;
  584. dst->mb_mean = src->mb_mean;
  585. dst->mbskip_table = src->mbskip_table;
  586. dst->qscale_table = src->qscale_table;
  587. dst->mb_type = src->mb_type;
  588. for (i = 0; i < 2; i++) {
  589. dst->motion_val[i] = src->motion_val[i];
  590. dst->ref_index[i] = src->ref_index[i];
  591. }
  592. dst->alloc_mb_width = src->alloc_mb_width;
  593. dst->alloc_mb_height = src->alloc_mb_height;
  594. return 0;
  595. }
  596. int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
  597. {
  598. int ret;
  599. av_assert0(!dst->f.buf[0]);
  600. av_assert0(src->f.buf[0]);
  601. src->tf.f = &src->f;
  602. dst->tf.f = &dst->f;
  603. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  604. if (ret < 0)
  605. goto fail;
  606. ret = update_picture_tables(dst, src);
  607. if (ret < 0)
  608. goto fail;
  609. if (src->hwaccel_picture_private) {
  610. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  611. if (!dst->hwaccel_priv_buf)
  612. goto fail;
  613. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  614. }
  615. dst->field_picture = src->field_picture;
  616. dst->mb_var_sum = src->mb_var_sum;
  617. dst->mc_mb_var_sum = src->mc_mb_var_sum;
  618. dst->b_frame_score = src->b_frame_score;
  619. dst->needs_realloc = src->needs_realloc;
  620. dst->reference = src->reference;
  621. dst->shared = src->shared;
  622. return 0;
  623. fail:
  624. ff_mpeg_unref_picture(s, dst);
  625. return ret;
  626. }
  627. static void exchange_uv(MpegEncContext *s)
  628. {
  629. int16_t (*tmp)[64];
  630. tmp = s->pblocks[4];
  631. s->pblocks[4] = s->pblocks[5];
  632. s->pblocks[5] = tmp;
  633. }
  634. static int init_duplicate_context(MpegEncContext *s)
  635. {
  636. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  637. int c_size = s->mb_stride * (s->mb_height + 1);
  638. int yc_size = y_size + 2 * c_size;
  639. int i;
  640. if (s->mb_height & 1)
  641. yc_size += 2*s->b8_stride + 2*s->mb_stride;
  642. s->edge_emu_buffer =
  643. s->me.scratchpad =
  644. s->me.temp =
  645. s->rd_scratchpad =
  646. s->b_scratchpad =
  647. s->obmc_scratchpad = NULL;
  648. if (s->encoding) {
  649. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  650. ME_MAP_SIZE * sizeof(uint32_t), fail)
  651. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  652. ME_MAP_SIZE * sizeof(uint32_t), fail)
  653. if (s->avctx->noise_reduction) {
  654. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  655. 2 * 64 * sizeof(int), fail)
  656. }
  657. }
  658. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
  659. s->block = s->blocks[0];
  660. for (i = 0; i < 12; i++) {
  661. s->pblocks[i] = &s->block[i];
  662. }
  663. if (s->avctx->codec_tag == AV_RL32("VCR2"))
  664. exchange_uv(s);
  665. if (s->out_format == FMT_H263) {
  666. /* ac values */
  667. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  668. yc_size * sizeof(int16_t) * 16, fail);
  669. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  670. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  671. s->ac_val[2] = s->ac_val[1] + c_size;
  672. }
  673. return 0;
  674. fail:
  675. return -1; // free() through ff_MPV_common_end()
  676. }
  677. static void free_duplicate_context(MpegEncContext *s)
  678. {
  679. if (s == NULL)
  680. return;
  681. av_freep(&s->edge_emu_buffer);
  682. av_freep(&s->me.scratchpad);
  683. s->me.temp =
  684. s->rd_scratchpad =
  685. s->b_scratchpad =
  686. s->obmc_scratchpad = NULL;
  687. av_freep(&s->dct_error_sum);
  688. av_freep(&s->me.map);
  689. av_freep(&s->me.score_map);
  690. av_freep(&s->blocks);
  691. av_freep(&s->ac_val_base);
  692. s->block = NULL;
  693. }
  694. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  695. {
  696. #define COPY(a) bak->a = src->a
  697. COPY(edge_emu_buffer);
  698. COPY(me.scratchpad);
  699. COPY(me.temp);
  700. COPY(rd_scratchpad);
  701. COPY(b_scratchpad);
  702. COPY(obmc_scratchpad);
  703. COPY(me.map);
  704. COPY(me.score_map);
  705. COPY(blocks);
  706. COPY(block);
  707. COPY(start_mb_y);
  708. COPY(end_mb_y);
  709. COPY(me.map_generation);
  710. COPY(pb);
  711. COPY(dct_error_sum);
  712. COPY(dct_count[0]);
  713. COPY(dct_count[1]);
  714. COPY(ac_val_base);
  715. COPY(ac_val[0]);
  716. COPY(ac_val[1]);
  717. COPY(ac_val[2]);
  718. #undef COPY
  719. }
  720. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  721. {
  722. MpegEncContext bak;
  723. int i, ret;
  724. // FIXME copy only needed parts
  725. // START_TIMER
  726. backup_duplicate_context(&bak, dst);
  727. memcpy(dst, src, sizeof(MpegEncContext));
  728. backup_duplicate_context(dst, &bak);
  729. for (i = 0; i < 12; i++) {
  730. dst->pblocks[i] = &dst->block[i];
  731. }
  732. if (dst->avctx->codec_tag == AV_RL32("VCR2"))
  733. exchange_uv(dst);
  734. if (!dst->edge_emu_buffer &&
  735. (ret = frame_size_alloc(dst, dst->linesize)) < 0) {
  736. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  737. "scratch buffers.\n");
  738. return ret;
  739. }
  740. // STOP_TIMER("update_duplicate_context")
  741. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  742. return 0;
  743. }
  744. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  745. const AVCodecContext *src)
  746. {
  747. int i, ret;
  748. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  749. if (dst == src)
  750. return 0;
  751. av_assert0(s != s1);
  752. // FIXME can parameters change on I-frames?
  753. // in that case dst may need a reinit
  754. if (!s->context_initialized) {
  755. memcpy(s, s1, sizeof(MpegEncContext));
  756. s->avctx = dst;
  757. s->bitstream_buffer = NULL;
  758. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  759. if (s1->context_initialized){
  760. // s->picture_range_start += MAX_PICTURE_COUNT;
  761. // s->picture_range_end += MAX_PICTURE_COUNT;
  762. if((ret = ff_MPV_common_init(s)) < 0){
  763. memset(s, 0, sizeof(MpegEncContext));
  764. s->avctx = dst;
  765. return ret;
  766. }
  767. }
  768. }
  769. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  770. s->context_reinit = 0;
  771. s->height = s1->height;
  772. s->width = s1->width;
  773. if ((ret = ff_MPV_common_frame_size_change(s)) < 0)
  774. return ret;
  775. }
  776. s->avctx->coded_height = s1->avctx->coded_height;
  777. s->avctx->coded_width = s1->avctx->coded_width;
  778. s->avctx->width = s1->avctx->width;
  779. s->avctx->height = s1->avctx->height;
  780. s->coded_picture_number = s1->coded_picture_number;
  781. s->picture_number = s1->picture_number;
  782. av_assert0(!s->picture || s->picture != s1->picture);
  783. if(s->picture)
  784. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  785. ff_mpeg_unref_picture(s, &s->picture[i]);
  786. if (s1->picture[i].f.buf[0] &&
  787. (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
  788. return ret;
  789. }
  790. #define UPDATE_PICTURE(pic)\
  791. do {\
  792. ff_mpeg_unref_picture(s, &s->pic);\
  793. if (s1->pic.f.buf[0])\
  794. ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
  795. else\
  796. ret = update_picture_tables(&s->pic, &s1->pic);\
  797. if (ret < 0)\
  798. return ret;\
  799. } while (0)
  800. UPDATE_PICTURE(current_picture);
  801. UPDATE_PICTURE(last_picture);
  802. UPDATE_PICTURE(next_picture);
  803. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  804. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  805. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  806. // Error/bug resilience
  807. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  808. s->workaround_bugs = s1->workaround_bugs;
  809. s->padding_bug_score = s1->padding_bug_score;
  810. // MPEG4 timing info
  811. memcpy(&s->last_time_base, &s1->last_time_base,
  812. (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
  813. (char *) &s1->last_time_base);
  814. // B-frame info
  815. s->max_b_frames = s1->max_b_frames;
  816. s->low_delay = s1->low_delay;
  817. s->droppable = s1->droppable;
  818. // DivX handling (doesn't work)
  819. s->divx_packed = s1->divx_packed;
  820. if (s1->bitstream_buffer) {
  821. if (s1->bitstream_buffer_size +
  822. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  823. av_fast_malloc(&s->bitstream_buffer,
  824. &s->allocated_bitstream_buffer_size,
  825. s1->allocated_bitstream_buffer_size);
  826. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  827. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  828. s1->bitstream_buffer_size);
  829. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  830. FF_INPUT_BUFFER_PADDING_SIZE);
  831. }
  832. // linesize dependend scratch buffer allocation
  833. if (!s->edge_emu_buffer)
  834. if (s1->linesize) {
  835. if (frame_size_alloc(s, s1->linesize) < 0) {
  836. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  837. "scratch buffers.\n");
  838. return AVERROR(ENOMEM);
  839. }
  840. } else {
  841. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  842. "be allocated due to unknown size.\n");
  843. }
  844. // MPEG2/interlacing info
  845. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  846. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  847. if (!s1->first_field) {
  848. s->last_pict_type = s1->pict_type;
  849. if (s1->current_picture_ptr)
  850. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
  851. }
  852. return 0;
  853. }
  854. /**
  855. * Set the given MpegEncContext to common defaults
  856. * (same for encoding and decoding).
  857. * The changed fields will not depend upon the
  858. * prior state of the MpegEncContext.
  859. */
  860. void ff_MPV_common_defaults(MpegEncContext *s)
  861. {
  862. s->y_dc_scale_table =
  863. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  864. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  865. s->progressive_frame = 1;
  866. s->progressive_sequence = 1;
  867. s->picture_structure = PICT_FRAME;
  868. s->coded_picture_number = 0;
  869. s->picture_number = 0;
  870. s->f_code = 1;
  871. s->b_code = 1;
  872. s->slice_context_count = 1;
  873. }
  874. /**
  875. * Set the given MpegEncContext to defaults for decoding.
  876. * the changed fields will not depend upon
  877. * the prior state of the MpegEncContext.
  878. */
  879. void ff_MPV_decode_defaults(MpegEncContext *s)
  880. {
  881. ff_MPV_common_defaults(s);
  882. }
  883. static int init_er(MpegEncContext *s)
  884. {
  885. ERContext *er = &s->er;
  886. int mb_array_size = s->mb_height * s->mb_stride;
  887. int i;
  888. er->avctx = s->avctx;
  889. er->dsp = &s->dsp;
  890. er->mb_index2xy = s->mb_index2xy;
  891. er->mb_num = s->mb_num;
  892. er->mb_width = s->mb_width;
  893. er->mb_height = s->mb_height;
  894. er->mb_stride = s->mb_stride;
  895. er->b8_stride = s->b8_stride;
  896. er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
  897. er->error_status_table = av_mallocz(mb_array_size);
  898. if (!er->er_temp_buffer || !er->error_status_table)
  899. goto fail;
  900. er->mbskip_table = s->mbskip_table;
  901. er->mbintra_table = s->mbintra_table;
  902. for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
  903. er->dc_val[i] = s->dc_val[i];
  904. er->decode_mb = mpeg_er_decode_mb;
  905. er->opaque = s;
  906. return 0;
  907. fail:
  908. av_freep(&er->er_temp_buffer);
  909. av_freep(&er->error_status_table);
  910. return AVERROR(ENOMEM);
  911. }
  912. /**
  913. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  914. */
  915. static int init_context_frame(MpegEncContext *s)
  916. {
  917. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  918. s->mb_width = (s->width + 15) / 16;
  919. s->mb_stride = s->mb_width + 1;
  920. s->b8_stride = s->mb_width * 2 + 1;
  921. s->b4_stride = s->mb_width * 4 + 1;
  922. mb_array_size = s->mb_height * s->mb_stride;
  923. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  924. /* set default edge pos, will be overriden
  925. * in decode_header if needed */
  926. s->h_edge_pos = s->mb_width * 16;
  927. s->v_edge_pos = s->mb_height * 16;
  928. s->mb_num = s->mb_width * s->mb_height;
  929. s->block_wrap[0] =
  930. s->block_wrap[1] =
  931. s->block_wrap[2] =
  932. s->block_wrap[3] = s->b8_stride;
  933. s->block_wrap[4] =
  934. s->block_wrap[5] = s->mb_stride;
  935. y_size = s->b8_stride * (2 * s->mb_height + 1);
  936. c_size = s->mb_stride * (s->mb_height + 1);
  937. yc_size = y_size + 2 * c_size;
  938. if (s->mb_height & 1)
  939. yc_size += 2*s->b8_stride + 2*s->mb_stride;
  940. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
  941. for (y = 0; y < s->mb_height; y++)
  942. for (x = 0; x < s->mb_width; x++)
  943. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  944. s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  945. if (s->encoding) {
  946. /* Allocate MV tables */
  947. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  948. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  949. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  950. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  951. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  952. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  953. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  954. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  955. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  956. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  957. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  958. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  959. /* Allocate MB type table */
  960. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
  961. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
  962. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  963. mb_array_size * sizeof(float), fail);
  964. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  965. mb_array_size * sizeof(float), fail);
  966. }
  967. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  968. (s->flags & CODEC_FLAG_INTERLACED_ME)) {
  969. /* interlaced direct mode decoding tables */
  970. for (i = 0; i < 2; i++) {
  971. int j, k;
  972. for (j = 0; j < 2; j++) {
  973. for (k = 0; k < 2; k++) {
  974. FF_ALLOCZ_OR_GOTO(s->avctx,
  975. s->b_field_mv_table_base[i][j][k],
  976. mv_table_size * 2 * sizeof(int16_t),
  977. fail);
  978. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  979. s->mb_stride + 1;
  980. }
  981. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
  982. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
  983. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
  984. }
  985. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
  986. }
  987. }
  988. if (s->out_format == FMT_H263) {
  989. /* cbp values */
  990. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size + (s->mb_height&1)*2*s->b8_stride, fail);
  991. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  992. /* cbp, ac_pred, pred_dir */
  993. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail);
  994. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
  995. }
  996. if (s->h263_pred || s->h263_plus || !s->encoding) {
  997. /* dc values */
  998. // MN: we need these for error resilience of intra-frames
  999. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
  1000. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  1001. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  1002. s->dc_val[2] = s->dc_val[1] + c_size;
  1003. for (i = 0; i < yc_size; i++)
  1004. s->dc_val_base[i] = 1024;
  1005. }
  1006. /* which mb is a intra block */
  1007. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  1008. memset(s->mbintra_table, 1, mb_array_size);
  1009. /* init macroblock skip table */
  1010. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  1011. // Note the + 1 is for a quicker mpeg4 slice_end detection
  1012. return init_er(s);
  1013. fail:
  1014. return AVERROR(ENOMEM);
  1015. }
  1016. /**
  1017. * init common structure for both encoder and decoder.
  1018. * this assumes that some variables like width/height are already set
  1019. */
  1020. av_cold int ff_MPV_common_init(MpegEncContext *s)
  1021. {
  1022. int i;
  1023. int nb_slices = (HAVE_THREADS &&
  1024. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  1025. s->avctx->thread_count : 1;
  1026. if (s->encoding && s->avctx->slices)
  1027. nb_slices = s->avctx->slices;
  1028. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1029. s->mb_height = (s->height + 31) / 32 * 2;
  1030. else
  1031. s->mb_height = (s->height + 15) / 16;
  1032. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  1033. av_log(s->avctx, AV_LOG_ERROR,
  1034. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  1035. return -1;
  1036. }
  1037. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  1038. int max_slices;
  1039. if (s->mb_height)
  1040. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  1041. else
  1042. max_slices = MAX_THREADS;
  1043. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  1044. " reducing to %d\n", nb_slices, max_slices);
  1045. nb_slices = max_slices;
  1046. }
  1047. if ((s->width || s->height) &&
  1048. av_image_check_size(s->width, s->height, 0, s->avctx))
  1049. return -1;
  1050. ff_dct_common_init(s);
  1051. s->flags = s->avctx->flags;
  1052. s->flags2 = s->avctx->flags2;
  1053. /* set chroma shifts */
  1054. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,
  1055. &s->chroma_x_shift,
  1056. &s->chroma_y_shift);
  1057. /* convert fourcc to upper case */
  1058. s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
  1059. s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
  1060. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  1061. MAX_PICTURE_COUNT * sizeof(Picture), fail);
  1062. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1063. av_frame_unref(&s->picture[i].f);
  1064. }
  1065. memset(&s->next_picture, 0, sizeof(s->next_picture));
  1066. memset(&s->last_picture, 0, sizeof(s->last_picture));
  1067. memset(&s->current_picture, 0, sizeof(s->current_picture));
  1068. av_frame_unref(&s->next_picture.f);
  1069. av_frame_unref(&s->last_picture.f);
  1070. av_frame_unref(&s->current_picture.f);
  1071. if (init_context_frame(s))
  1072. goto fail;
  1073. s->parse_context.state = -1;
  1074. s->context_initialized = 1;
  1075. s->thread_context[0] = s;
  1076. // if (s->width && s->height) {
  1077. if (nb_slices > 1) {
  1078. for (i = 1; i < nb_slices; i++) {
  1079. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1080. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1081. }
  1082. for (i = 0; i < nb_slices; i++) {
  1083. if (init_duplicate_context(s->thread_context[i]) < 0)
  1084. goto fail;
  1085. s->thread_context[i]->start_mb_y =
  1086. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1087. s->thread_context[i]->end_mb_y =
  1088. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1089. }
  1090. } else {
  1091. if (init_duplicate_context(s) < 0)
  1092. goto fail;
  1093. s->start_mb_y = 0;
  1094. s->end_mb_y = s->mb_height;
  1095. }
  1096. s->slice_context_count = nb_slices;
  1097. // }
  1098. return 0;
  1099. fail:
  1100. ff_MPV_common_end(s);
  1101. return -1;
  1102. }
  1103. /**
  1104. * Frees and resets MpegEncContext fields depending on the resolution.
  1105. * Is used during resolution changes to avoid a full reinitialization of the
  1106. * codec.
  1107. */
  1108. static int free_context_frame(MpegEncContext *s)
  1109. {
  1110. int i, j, k;
  1111. av_freep(&s->mb_type);
  1112. av_freep(&s->p_mv_table_base);
  1113. av_freep(&s->b_forw_mv_table_base);
  1114. av_freep(&s->b_back_mv_table_base);
  1115. av_freep(&s->b_bidir_forw_mv_table_base);
  1116. av_freep(&s->b_bidir_back_mv_table_base);
  1117. av_freep(&s->b_direct_mv_table_base);
  1118. s->p_mv_table = NULL;
  1119. s->b_forw_mv_table = NULL;
  1120. s->b_back_mv_table = NULL;
  1121. s->b_bidir_forw_mv_table = NULL;
  1122. s->b_bidir_back_mv_table = NULL;
  1123. s->b_direct_mv_table = NULL;
  1124. for (i = 0; i < 2; i++) {
  1125. for (j = 0; j < 2; j++) {
  1126. for (k = 0; k < 2; k++) {
  1127. av_freep(&s->b_field_mv_table_base[i][j][k]);
  1128. s->b_field_mv_table[i][j][k] = NULL;
  1129. }
  1130. av_freep(&s->b_field_select_table[i][j]);
  1131. av_freep(&s->p_field_mv_table_base[i][j]);
  1132. s->p_field_mv_table[i][j] = NULL;
  1133. }
  1134. av_freep(&s->p_field_select_table[i]);
  1135. }
  1136. av_freep(&s->dc_val_base);
  1137. av_freep(&s->coded_block_base);
  1138. av_freep(&s->mbintra_table);
  1139. av_freep(&s->cbp_table);
  1140. av_freep(&s->pred_dir_table);
  1141. av_freep(&s->mbskip_table);
  1142. av_freep(&s->er.error_status_table);
  1143. av_freep(&s->er.er_temp_buffer);
  1144. av_freep(&s->mb_index2xy);
  1145. av_freep(&s->lambda_table);
  1146. av_freep(&s->cplx_tab);
  1147. av_freep(&s->bits_tab);
  1148. s->linesize = s->uvlinesize = 0;
  1149. return 0;
  1150. }
  1151. int ff_MPV_common_frame_size_change(MpegEncContext *s)
  1152. {
  1153. int i, err = 0;
  1154. if (s->slice_context_count > 1) {
  1155. for (i = 0; i < s->slice_context_count; i++) {
  1156. free_duplicate_context(s->thread_context[i]);
  1157. }
  1158. for (i = 1; i < s->slice_context_count; i++) {
  1159. av_freep(&s->thread_context[i]);
  1160. }
  1161. } else
  1162. free_duplicate_context(s);
  1163. if ((err = free_context_frame(s)) < 0)
  1164. return err;
  1165. if (s->picture)
  1166. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1167. s->picture[i].needs_realloc = 1;
  1168. }
  1169. s->last_picture_ptr =
  1170. s->next_picture_ptr =
  1171. s->current_picture_ptr = NULL;
  1172. // init
  1173. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1174. s->mb_height = (s->height + 31) / 32 * 2;
  1175. else
  1176. s->mb_height = (s->height + 15) / 16;
  1177. if ((s->width || s->height) &&
  1178. av_image_check_size(s->width, s->height, 0, s->avctx))
  1179. return AVERROR_INVALIDDATA;
  1180. if ((err = init_context_frame(s)))
  1181. goto fail;
  1182. s->thread_context[0] = s;
  1183. if (s->width && s->height) {
  1184. int nb_slices = s->slice_context_count;
  1185. if (nb_slices > 1) {
  1186. for (i = 1; i < nb_slices; i++) {
  1187. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1188. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1189. }
  1190. for (i = 0; i < nb_slices; i++) {
  1191. if (init_duplicate_context(s->thread_context[i]) < 0)
  1192. goto fail;
  1193. s->thread_context[i]->start_mb_y =
  1194. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1195. s->thread_context[i]->end_mb_y =
  1196. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1197. }
  1198. } else {
  1199. err = init_duplicate_context(s);
  1200. if (err < 0)
  1201. goto fail;
  1202. s->start_mb_y = 0;
  1203. s->end_mb_y = s->mb_height;
  1204. }
  1205. s->slice_context_count = nb_slices;
  1206. }
  1207. return 0;
  1208. fail:
  1209. ff_MPV_common_end(s);
  1210. return err;
  1211. }
  1212. /* init common structure for both encoder and decoder */
  1213. void ff_MPV_common_end(MpegEncContext *s)
  1214. {
  1215. int i;
  1216. if (s->slice_context_count > 1) {
  1217. for (i = 0; i < s->slice_context_count; i++) {
  1218. free_duplicate_context(s->thread_context[i]);
  1219. }
  1220. for (i = 1; i < s->slice_context_count; i++) {
  1221. av_freep(&s->thread_context[i]);
  1222. }
  1223. s->slice_context_count = 1;
  1224. } else free_duplicate_context(s);
  1225. av_freep(&s->parse_context.buffer);
  1226. s->parse_context.buffer_size = 0;
  1227. av_freep(&s->bitstream_buffer);
  1228. s->allocated_bitstream_buffer_size = 0;
  1229. if (s->picture) {
  1230. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1231. ff_free_picture_tables(&s->picture[i]);
  1232. ff_mpeg_unref_picture(s, &s->picture[i]);
  1233. }
  1234. }
  1235. av_freep(&s->picture);
  1236. ff_free_picture_tables(&s->last_picture);
  1237. ff_mpeg_unref_picture(s, &s->last_picture);
  1238. ff_free_picture_tables(&s->current_picture);
  1239. ff_mpeg_unref_picture(s, &s->current_picture);
  1240. ff_free_picture_tables(&s->next_picture);
  1241. ff_mpeg_unref_picture(s, &s->next_picture);
  1242. free_context_frame(s);
  1243. s->context_initialized = 0;
  1244. s->last_picture_ptr =
  1245. s->next_picture_ptr =
  1246. s->current_picture_ptr = NULL;
  1247. s->linesize = s->uvlinesize = 0;
  1248. }
  1249. av_cold void ff_init_rl(RLTable *rl,
  1250. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  1251. {
  1252. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  1253. uint8_t index_run[MAX_RUN + 1];
  1254. int last, run, level, start, end, i;
  1255. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  1256. if (static_store && rl->max_level[0])
  1257. return;
  1258. /* compute max_level[], max_run[] and index_run[] */
  1259. for (last = 0; last < 2; last++) {
  1260. if (last == 0) {
  1261. start = 0;
  1262. end = rl->last;
  1263. } else {
  1264. start = rl->last;
  1265. end = rl->n;
  1266. }
  1267. memset(max_level, 0, MAX_RUN + 1);
  1268. memset(max_run, 0, MAX_LEVEL + 1);
  1269. memset(index_run, rl->n, MAX_RUN + 1);
  1270. for (i = start; i < end; i++) {
  1271. run = rl->table_run[i];
  1272. level = rl->table_level[i];
  1273. if (index_run[run] == rl->n)
  1274. index_run[run] = i;
  1275. if (level > max_level[run])
  1276. max_level[run] = level;
  1277. if (run > max_run[level])
  1278. max_run[level] = run;
  1279. }
  1280. if (static_store)
  1281. rl->max_level[last] = static_store[last];
  1282. else
  1283. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1284. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1285. if (static_store)
  1286. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  1287. else
  1288. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1289. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1290. if (static_store)
  1291. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  1292. else
  1293. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1294. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1295. }
  1296. }
  1297. av_cold void ff_init_vlc_rl(RLTable *rl)
  1298. {
  1299. int i, q;
  1300. for (q = 0; q < 32; q++) {
  1301. int qmul = q * 2;
  1302. int qadd = (q - 1) | 1;
  1303. if (q == 0) {
  1304. qmul = 1;
  1305. qadd = 0;
  1306. }
  1307. for (i = 0; i < rl->vlc.table_size; i++) {
  1308. int code = rl->vlc.table[i][0];
  1309. int len = rl->vlc.table[i][1];
  1310. int level, run;
  1311. if (len == 0) { // illegal code
  1312. run = 66;
  1313. level = MAX_LEVEL;
  1314. } else if (len < 0) { // more bits needed
  1315. run = 0;
  1316. level = code;
  1317. } else {
  1318. if (code == rl->n) { // esc
  1319. run = 66;
  1320. level = 0;
  1321. } else {
  1322. run = rl->table_run[code] + 1;
  1323. level = rl->table_level[code] * qmul + qadd;
  1324. if (code >= rl->last) run += 192;
  1325. }
  1326. }
  1327. rl->rl_vlc[q][i].len = len;
  1328. rl->rl_vlc[q][i].level = level;
  1329. rl->rl_vlc[q][i].run = run;
  1330. }
  1331. }
  1332. }
  1333. static void release_unused_pictures(MpegEncContext *s)
  1334. {
  1335. int i;
  1336. /* release non reference frames */
  1337. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1338. if (!s->picture[i].reference)
  1339. ff_mpeg_unref_picture(s, &s->picture[i]);
  1340. }
  1341. }
  1342. static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
  1343. {
  1344. if (pic == s->last_picture_ptr)
  1345. return 0;
  1346. if (pic->f.buf[0] == NULL)
  1347. return 1;
  1348. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  1349. return 1;
  1350. return 0;
  1351. }
  1352. static int find_unused_picture(MpegEncContext *s, int shared)
  1353. {
  1354. int i;
  1355. if (shared) {
  1356. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1357. if (s->picture[i].f.buf[0] == NULL && &s->picture[i] != s->last_picture_ptr)
  1358. return i;
  1359. }
  1360. } else {
  1361. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1362. if (pic_is_unused(s, &s->picture[i]))
  1363. return i;
  1364. }
  1365. }
  1366. av_log(s->avctx, AV_LOG_FATAL,
  1367. "Internal error, picture buffer overflow\n");
  1368. /* We could return -1, but the codec would crash trying to draw into a
  1369. * non-existing frame anyway. This is safer than waiting for a random crash.
  1370. * Also the return of this is never useful, an encoder must only allocate
  1371. * as much as allowed in the specification. This has no relationship to how
  1372. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  1373. * enough for such valid streams).
  1374. * Plus, a decoder has to check stream validity and remove frames if too
  1375. * many reference frames are around. Waiting for "OOM" is not correct at
  1376. * all. Similarly, missing reference frames have to be replaced by
  1377. * interpolated/MC frames, anything else is a bug in the codec ...
  1378. */
  1379. abort();
  1380. return -1;
  1381. }
  1382. int ff_find_unused_picture(MpegEncContext *s, int shared)
  1383. {
  1384. int ret = find_unused_picture(s, shared);
  1385. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  1386. if (s->picture[ret].needs_realloc) {
  1387. s->picture[ret].needs_realloc = 0;
  1388. ff_free_picture_tables(&s->picture[ret]);
  1389. ff_mpeg_unref_picture(s, &s->picture[ret]);
  1390. }
  1391. }
  1392. return ret;
  1393. }
  1394. /**
  1395. * generic function called after decoding
  1396. * the header and before a frame is decoded.
  1397. */
  1398. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1399. {
  1400. int i, ret;
  1401. Picture *pic;
  1402. s->mb_skipped = 0;
  1403. if (!ff_thread_can_start_frame(avctx)) {
  1404. av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
  1405. return -1;
  1406. }
  1407. /* mark & release old frames */
  1408. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1409. s->last_picture_ptr != s->next_picture_ptr &&
  1410. s->last_picture_ptr->f.buf[0]) {
  1411. ff_mpeg_unref_picture(s, s->last_picture_ptr);
  1412. }
  1413. /* release forgotten pictures */
  1414. /* if (mpeg124/h263) */
  1415. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1416. if (&s->picture[i] != s->last_picture_ptr &&
  1417. &s->picture[i] != s->next_picture_ptr &&
  1418. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1419. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1420. av_log(avctx, AV_LOG_ERROR,
  1421. "releasing zombie picture\n");
  1422. ff_mpeg_unref_picture(s, &s->picture[i]);
  1423. }
  1424. }
  1425. ff_mpeg_unref_picture(s, &s->current_picture);
  1426. release_unused_pictures(s);
  1427. if (s->current_picture_ptr &&
  1428. s->current_picture_ptr->f.buf[0] == NULL) {
  1429. // we already have a unused image
  1430. // (maybe it was set before reading the header)
  1431. pic = s->current_picture_ptr;
  1432. } else {
  1433. i = ff_find_unused_picture(s, 0);
  1434. if (i < 0) {
  1435. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1436. return i;
  1437. }
  1438. pic = &s->picture[i];
  1439. }
  1440. pic->reference = 0;
  1441. if (!s->droppable) {
  1442. if (s->pict_type != AV_PICTURE_TYPE_B)
  1443. pic->reference = 3;
  1444. }
  1445. pic->f.coded_picture_number = s->coded_picture_number++;
  1446. if (ff_alloc_picture(s, pic, 0) < 0)
  1447. return -1;
  1448. s->current_picture_ptr = pic;
  1449. // FIXME use only the vars from current_pic
  1450. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  1451. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1452. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1453. if (s->picture_structure != PICT_FRAME)
  1454. s->current_picture_ptr->f.top_field_first =
  1455. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1456. }
  1457. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
  1458. !s->progressive_sequence;
  1459. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1460. s->current_picture_ptr->f.pict_type = s->pict_type;
  1461. // if (s->flags && CODEC_FLAG_QSCALE)
  1462. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1463. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1464. if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
  1465. s->current_picture_ptr)) < 0)
  1466. return ret;
  1467. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1468. s->last_picture_ptr = s->next_picture_ptr;
  1469. if (!s->droppable)
  1470. s->next_picture_ptr = s->current_picture_ptr;
  1471. }
  1472. av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1473. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1474. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  1475. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  1476. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  1477. s->pict_type, s->droppable);
  1478. if ((s->last_picture_ptr == NULL ||
  1479. s->last_picture_ptr->f.buf[0] == NULL) &&
  1480. (s->pict_type != AV_PICTURE_TYPE_I ||
  1481. s->picture_structure != PICT_FRAME)) {
  1482. int h_chroma_shift, v_chroma_shift;
  1483. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1484. &h_chroma_shift, &v_chroma_shift);
  1485. if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f.buf[0])
  1486. av_log(avctx, AV_LOG_DEBUG,
  1487. "allocating dummy last picture for B frame\n");
  1488. else if (s->pict_type != AV_PICTURE_TYPE_I)
  1489. av_log(avctx, AV_LOG_ERROR,
  1490. "warning: first frame is no keyframe\n");
  1491. else if (s->picture_structure != PICT_FRAME)
  1492. av_log(avctx, AV_LOG_DEBUG,
  1493. "allocate dummy last picture for field based first keyframe\n");
  1494. /* Allocate a dummy frame */
  1495. i = ff_find_unused_picture(s, 0);
  1496. if (i < 0) {
  1497. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1498. return i;
  1499. }
  1500. s->last_picture_ptr = &s->picture[i];
  1501. s->last_picture_ptr->reference = 3;
  1502. s->last_picture_ptr->f.key_frame = 0;
  1503. s->last_picture_ptr->f.pict_type = AV_PICTURE_TYPE_P;
  1504. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1505. s->last_picture_ptr = NULL;
  1506. return -1;
  1507. }
  1508. memset(s->last_picture_ptr->f.data[0], 0x80,
  1509. avctx->height * s->last_picture_ptr->f.linesize[0]);
  1510. memset(s->last_picture_ptr->f.data[1], 0x80,
  1511. (avctx->height >> v_chroma_shift) *
  1512. s->last_picture_ptr->f.linesize[1]);
  1513. memset(s->last_picture_ptr->f.data[2], 0x80,
  1514. (avctx->height >> v_chroma_shift) *
  1515. s->last_picture_ptr->f.linesize[2]);
  1516. if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
  1517. for(i=0; i<avctx->height; i++)
  1518. memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i, 16, avctx->width);
  1519. }
  1520. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1521. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1522. }
  1523. if ((s->next_picture_ptr == NULL ||
  1524. s->next_picture_ptr->f.buf[0] == NULL) &&
  1525. s->pict_type == AV_PICTURE_TYPE_B) {
  1526. /* Allocate a dummy frame */
  1527. i = ff_find_unused_picture(s, 0);
  1528. if (i < 0) {
  1529. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1530. return i;
  1531. }
  1532. s->next_picture_ptr = &s->picture[i];
  1533. s->next_picture_ptr->reference = 3;
  1534. s->next_picture_ptr->f.key_frame = 0;
  1535. s->next_picture_ptr->f.pict_type = AV_PICTURE_TYPE_P;
  1536. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1537. s->next_picture_ptr = NULL;
  1538. return -1;
  1539. }
  1540. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1541. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1542. }
  1543. #if 0 // BUFREF-FIXME
  1544. memset(s->last_picture.f.data, 0, sizeof(s->last_picture.f.data));
  1545. memset(s->next_picture.f.data, 0, sizeof(s->next_picture.f.data));
  1546. #endif
  1547. if (s->last_picture_ptr) {
  1548. ff_mpeg_unref_picture(s, &s->last_picture);
  1549. if (s->last_picture_ptr->f.buf[0] &&
  1550. (ret = ff_mpeg_ref_picture(s, &s->last_picture,
  1551. s->last_picture_ptr)) < 0)
  1552. return ret;
  1553. }
  1554. if (s->next_picture_ptr) {
  1555. ff_mpeg_unref_picture(s, &s->next_picture);
  1556. if (s->next_picture_ptr->f.buf[0] &&
  1557. (ret = ff_mpeg_ref_picture(s, &s->next_picture,
  1558. s->next_picture_ptr)) < 0)
  1559. return ret;
  1560. }
  1561. av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1562. s->last_picture_ptr->f.buf[0]));
  1563. if (s->picture_structure!= PICT_FRAME) {
  1564. int i;
  1565. for (i = 0; i < 4; i++) {
  1566. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1567. s->current_picture.f.data[i] +=
  1568. s->current_picture.f.linesize[i];
  1569. }
  1570. s->current_picture.f.linesize[i] *= 2;
  1571. s->last_picture.f.linesize[i] *= 2;
  1572. s->next_picture.f.linesize[i] *= 2;
  1573. }
  1574. }
  1575. s->err_recognition = avctx->err_recognition;
  1576. /* set dequantizer, we can't do it during init as
  1577. * it might change for mpeg4 and we can't do it in the header
  1578. * decode as init is not called for mpeg4 there yet */
  1579. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1580. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1581. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1582. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1583. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1584. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1585. } else {
  1586. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1587. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1588. }
  1589. return 0;
  1590. }
  1591. /* called after a frame has been decoded. */
  1592. void ff_MPV_frame_end(MpegEncContext *s)
  1593. {
  1594. emms_c();
  1595. if (s->current_picture.reference)
  1596. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1597. }
  1598. /**
  1599. * Draw a line from (ex, ey) -> (sx, sy).
  1600. * @param w width of the image
  1601. * @param h height of the image
  1602. * @param stride stride/linesize of the image
  1603. * @param color color of the arrow
  1604. */
  1605. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  1606. int w, int h, int stride, int color)
  1607. {
  1608. int x, y, fr, f;
  1609. sx = av_clip(sx, 0, w - 1);
  1610. sy = av_clip(sy, 0, h - 1);
  1611. ex = av_clip(ex, 0, w - 1);
  1612. ey = av_clip(ey, 0, h - 1);
  1613. buf[sy * stride + sx] += color;
  1614. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  1615. if (sx > ex) {
  1616. FFSWAP(int, sx, ex);
  1617. FFSWAP(int, sy, ey);
  1618. }
  1619. buf += sx + sy * stride;
  1620. ex -= sx;
  1621. f = ((ey - sy) << 16) / ex;
  1622. for (x = 0; x <= ex; x++) {
  1623. y = (x * f) >> 16;
  1624. fr = (x * f) & 0xFFFF;
  1625. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1626. if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  1627. }
  1628. } else {
  1629. if (sy > ey) {
  1630. FFSWAP(int, sx, ex);
  1631. FFSWAP(int, sy, ey);
  1632. }
  1633. buf += sx + sy * stride;
  1634. ey -= sy;
  1635. if (ey)
  1636. f = ((ex - sx) << 16) / ey;
  1637. else
  1638. f = 0;
  1639. for(y= 0; y <= ey; y++){
  1640. x = (y*f) >> 16;
  1641. fr = (y*f) & 0xFFFF;
  1642. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1643. if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
  1644. }
  1645. }
  1646. }
  1647. /**
  1648. * Draw an arrow from (ex, ey) -> (sx, sy).
  1649. * @param w width of the image
  1650. * @param h height of the image
  1651. * @param stride stride/linesize of the image
  1652. * @param color color of the arrow
  1653. */
  1654. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  1655. int ey, int w, int h, int stride, int color)
  1656. {
  1657. int dx,dy;
  1658. sx = av_clip(sx, -100, w + 100);
  1659. sy = av_clip(sy, -100, h + 100);
  1660. ex = av_clip(ex, -100, w + 100);
  1661. ey = av_clip(ey, -100, h + 100);
  1662. dx = ex - sx;
  1663. dy = ey - sy;
  1664. if (dx * dx + dy * dy > 3 * 3) {
  1665. int rx = dx + dy;
  1666. int ry = -dx + dy;
  1667. int length = ff_sqrt((rx * rx + ry * ry) << 8);
  1668. // FIXME subpixel accuracy
  1669. rx = ROUNDED_DIV(rx * 3 << 4, length);
  1670. ry = ROUNDED_DIV(ry * 3 << 4, length);
  1671. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1672. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1673. }
  1674. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1675. }
  1676. /**
  1677. * Print debugging info for the given picture.
  1678. */
  1679. void ff_print_debug_info2(AVCodecContext *avctx, Picture *p, AVFrame *pict, uint8_t *mbskip_table,
  1680. int *low_delay,
  1681. int mb_width, int mb_height, int mb_stride, int quarter_sample)
  1682. {
  1683. if (avctx->hwaccel || !p || !p->mb_type
  1684. || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
  1685. return;
  1686. if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1687. int x,y;
  1688. av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  1689. av_get_picture_type_char(pict->pict_type));
  1690. for (y = 0; y < mb_height; y++) {
  1691. for (x = 0; x < mb_width; x++) {
  1692. if (avctx->debug & FF_DEBUG_SKIP) {
  1693. int count = mbskip_table[x + y * mb_stride];
  1694. if (count > 9)
  1695. count = 9;
  1696. av_log(avctx, AV_LOG_DEBUG, "%1d", count);
  1697. }
  1698. if (avctx->debug & FF_DEBUG_QP) {
  1699. av_log(avctx, AV_LOG_DEBUG, "%2d",
  1700. p->qscale_table[x + y * mb_stride]);
  1701. }
  1702. if (avctx->debug & FF_DEBUG_MB_TYPE) {
  1703. int mb_type = p->mb_type[x + y * mb_stride];
  1704. // Type & MV direction
  1705. if (IS_PCM(mb_type))
  1706. av_log(avctx, AV_LOG_DEBUG, "P");
  1707. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1708. av_log(avctx, AV_LOG_DEBUG, "A");
  1709. else if (IS_INTRA4x4(mb_type))
  1710. av_log(avctx, AV_LOG_DEBUG, "i");
  1711. else if (IS_INTRA16x16(mb_type))
  1712. av_log(avctx, AV_LOG_DEBUG, "I");
  1713. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1714. av_log(avctx, AV_LOG_DEBUG, "d");
  1715. else if (IS_DIRECT(mb_type))
  1716. av_log(avctx, AV_LOG_DEBUG, "D");
  1717. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1718. av_log(avctx, AV_LOG_DEBUG, "g");
  1719. else if (IS_GMC(mb_type))
  1720. av_log(avctx, AV_LOG_DEBUG, "G");
  1721. else if (IS_SKIP(mb_type))
  1722. av_log(avctx, AV_LOG_DEBUG, "S");
  1723. else if (!USES_LIST(mb_type, 1))
  1724. av_log(avctx, AV_LOG_DEBUG, ">");
  1725. else if (!USES_LIST(mb_type, 0))
  1726. av_log(avctx, AV_LOG_DEBUG, "<");
  1727. else {
  1728. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1729. av_log(avctx, AV_LOG_DEBUG, "X");
  1730. }
  1731. // segmentation
  1732. if (IS_8X8(mb_type))
  1733. av_log(avctx, AV_LOG_DEBUG, "+");
  1734. else if (IS_16X8(mb_type))
  1735. av_log(avctx, AV_LOG_DEBUG, "-");
  1736. else if (IS_8X16(mb_type))
  1737. av_log(avctx, AV_LOG_DEBUG, "|");
  1738. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1739. av_log(avctx, AV_LOG_DEBUG, " ");
  1740. else
  1741. av_log(avctx, AV_LOG_DEBUG, "?");
  1742. if (IS_INTERLACED(mb_type))
  1743. av_log(avctx, AV_LOG_DEBUG, "=");
  1744. else
  1745. av_log(avctx, AV_LOG_DEBUG, " ");
  1746. }
  1747. }
  1748. av_log(avctx, AV_LOG_DEBUG, "\n");
  1749. }
  1750. }
  1751. if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1752. (avctx->debug_mv)) {
  1753. const int shift = 1 + quarter_sample;
  1754. int mb_y;
  1755. uint8_t *ptr;
  1756. int i;
  1757. int h_chroma_shift, v_chroma_shift, block_height;
  1758. const int width = avctx->width;
  1759. const int height = avctx->height;
  1760. const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
  1761. const int mv_stride = (mb_width << mv_sample_log2) +
  1762. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  1763. *low_delay = 0; // needed to see the vectors without trashing the buffers
  1764. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1765. av_frame_make_writable(pict);
  1766. pict->opaque = NULL;
  1767. ptr = pict->data[0];
  1768. block_height = 16 >> v_chroma_shift;
  1769. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  1770. int mb_x;
  1771. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  1772. const int mb_index = mb_x + mb_y * mb_stride;
  1773. if ((avctx->debug_mv) && p->motion_val[0]) {
  1774. int type;
  1775. for (type = 0; type < 3; type++) {
  1776. int direction = 0;
  1777. switch (type) {
  1778. case 0:
  1779. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
  1780. (pict->pict_type!= AV_PICTURE_TYPE_P))
  1781. continue;
  1782. direction = 0;
  1783. break;
  1784. case 1:
  1785. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
  1786. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1787. continue;
  1788. direction = 0;
  1789. break;
  1790. case 2:
  1791. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
  1792. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1793. continue;
  1794. direction = 1;
  1795. break;
  1796. }
  1797. if (!USES_LIST(p->mb_type[mb_index], direction))
  1798. continue;
  1799. if (IS_8X8(p->mb_type[mb_index])) {
  1800. int i;
  1801. for (i = 0; i < 4; i++) {
  1802. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1803. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1804. int xy = (mb_x * 2 + (i & 1) +
  1805. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1806. int mx = (p->motion_val[direction][xy][0] >> shift) + sx;
  1807. int my = (p->motion_val[direction][xy][1] >> shift) + sy;
  1808. draw_arrow(ptr, sx, sy, mx, my, width,
  1809. height, pict->linesize[0], 100);
  1810. }
  1811. } else if (IS_16X8(p->mb_type[mb_index])) {
  1812. int i;
  1813. for (i = 0; i < 2; i++) {
  1814. int sx = mb_x * 16 + 8;
  1815. int sy = mb_y * 16 + 4 + 8 * i;
  1816. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1817. int mx = (p->motion_val[direction][xy][0] >> shift);
  1818. int my = (p->motion_val[direction][xy][1] >> shift);
  1819. if (IS_INTERLACED(p->mb_type[mb_index]))
  1820. my *= 2;
  1821. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1822. height, pict->linesize[0], 100);
  1823. }
  1824. } else if (IS_8X16(p->mb_type[mb_index])) {
  1825. int i;
  1826. for (i = 0; i < 2; i++) {
  1827. int sx = mb_x * 16 + 4 + 8 * i;
  1828. int sy = mb_y * 16 + 8;
  1829. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1830. int mx = p->motion_val[direction][xy][0] >> shift;
  1831. int my = p->motion_val[direction][xy][1] >> shift;
  1832. if (IS_INTERLACED(p->mb_type[mb_index]))
  1833. my *= 2;
  1834. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1835. height, pict->linesize[0], 100);
  1836. }
  1837. } else {
  1838. int sx= mb_x * 16 + 8;
  1839. int sy= mb_y * 16 + 8;
  1840. int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1841. int mx= (p->motion_val[direction][xy][0]>>shift) + sx;
  1842. int my= (p->motion_val[direction][xy][1]>>shift) + sy;
  1843. draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100);
  1844. }
  1845. }
  1846. }
  1847. if ((avctx->debug & FF_DEBUG_VIS_QP)) {
  1848. uint64_t c = (p->qscale_table[mb_index] * 128 / 31) *
  1849. 0x0101010101010101ULL;
  1850. int y;
  1851. for (y = 0; y < block_height; y++) {
  1852. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1853. (block_height * mb_y + y) *
  1854. pict->linesize[1]) = c;
  1855. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1856. (block_height * mb_y + y) *
  1857. pict->linesize[2]) = c;
  1858. }
  1859. }
  1860. if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  1861. p->motion_val[0]) {
  1862. int mb_type = p->mb_type[mb_index];
  1863. uint64_t u,v;
  1864. int y;
  1865. #define COLOR(theta, r) \
  1866. u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
  1867. v = (int)(128 + r * sin(theta * 3.141592 / 180));
  1868. u = v = 128;
  1869. if (IS_PCM(mb_type)) {
  1870. COLOR(120, 48)
  1871. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  1872. IS_INTRA16x16(mb_type)) {
  1873. COLOR(30, 48)
  1874. } else if (IS_INTRA4x4(mb_type)) {
  1875. COLOR(90, 48)
  1876. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  1877. // COLOR(120, 48)
  1878. } else if (IS_DIRECT(mb_type)) {
  1879. COLOR(150, 48)
  1880. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  1881. COLOR(170, 48)
  1882. } else if (IS_GMC(mb_type)) {
  1883. COLOR(190, 48)
  1884. } else if (IS_SKIP(mb_type)) {
  1885. // COLOR(180, 48)
  1886. } else if (!USES_LIST(mb_type, 1)) {
  1887. COLOR(240, 48)
  1888. } else if (!USES_LIST(mb_type, 0)) {
  1889. COLOR(0, 48)
  1890. } else {
  1891. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1892. COLOR(300,48)
  1893. }
  1894. u *= 0x0101010101010101ULL;
  1895. v *= 0x0101010101010101ULL;
  1896. for (y = 0; y < block_height; y++) {
  1897. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1898. (block_height * mb_y + y) * pict->linesize[1]) = u;
  1899. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1900. (block_height * mb_y + y) * pict->linesize[2]) = v;
  1901. }
  1902. // segmentation
  1903. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  1904. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  1905. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1906. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  1907. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1908. }
  1909. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  1910. for (y = 0; y < 16; y++)
  1911. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  1912. pict->linesize[0]] ^= 0x80;
  1913. }
  1914. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  1915. int dm = 1 << (mv_sample_log2 - 2);
  1916. for (i = 0; i < 4; i++) {
  1917. int sx = mb_x * 16 + 8 * (i & 1);
  1918. int sy = mb_y * 16 + 8 * (i >> 1);
  1919. int xy = (mb_x * 2 + (i & 1) +
  1920. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1921. // FIXME bidir
  1922. int32_t *mv = (int32_t *) &p->motion_val[0][xy];
  1923. if (mv[0] != mv[dm] ||
  1924. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  1925. for (y = 0; y < 8; y++)
  1926. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  1927. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  1928. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  1929. pict->linesize[0]) ^= 0x8080808080808080ULL;
  1930. }
  1931. }
  1932. if (IS_INTERLACED(mb_type) &&
  1933. avctx->codec->id == AV_CODEC_ID_H264) {
  1934. // hmm
  1935. }
  1936. }
  1937. mbskip_table[mb_index] = 0;
  1938. }
  1939. }
  1940. }
  1941. }
  1942. void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
  1943. {
  1944. ff_print_debug_info2(s->avctx, p, pict, s->mbskip_table, &s->low_delay,
  1945. s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
  1946. }
  1947. int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
  1948. {
  1949. AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
  1950. int offset = 2*s->mb_stride + 1;
  1951. if(!ref)
  1952. return AVERROR(ENOMEM);
  1953. av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
  1954. ref->size -= offset;
  1955. ref->data += offset;
  1956. return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
  1957. }
  1958. static inline int hpel_motion_lowres(MpegEncContext *s,
  1959. uint8_t *dest, uint8_t *src,
  1960. int field_based, int field_select,
  1961. int src_x, int src_y,
  1962. int width, int height, ptrdiff_t stride,
  1963. int h_edge_pos, int v_edge_pos,
  1964. int w, int h, h264_chroma_mc_func *pix_op,
  1965. int motion_x, int motion_y)
  1966. {
  1967. const int lowres = s->avctx->lowres;
  1968. const int op_index = FFMIN(lowres, 3);
  1969. const int s_mask = (2 << lowres) - 1;
  1970. int emu = 0;
  1971. int sx, sy;
  1972. if (s->quarter_sample) {
  1973. motion_x /= 2;
  1974. motion_y /= 2;
  1975. }
  1976. sx = motion_x & s_mask;
  1977. sy = motion_y & s_mask;
  1978. src_x += motion_x >> lowres + 1;
  1979. src_y += motion_y >> lowres + 1;
  1980. src += src_y * stride + src_x;
  1981. if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
  1982. (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1983. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
  1984. s->linesize, s->linesize,
  1985. w + 1, (h + 1) << field_based,
  1986. src_x, src_y << field_based,
  1987. h_edge_pos, v_edge_pos);
  1988. src = s->edge_emu_buffer;
  1989. emu = 1;
  1990. }
  1991. sx = (sx << 2) >> lowres;
  1992. sy = (sy << 2) >> lowres;
  1993. if (field_select)
  1994. src += s->linesize;
  1995. pix_op[op_index](dest, src, stride, h, sx, sy);
  1996. return emu;
  1997. }
  1998. /* apply one mpeg motion vector to the three components */
  1999. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  2000. uint8_t *dest_y,
  2001. uint8_t *dest_cb,
  2002. uint8_t *dest_cr,
  2003. int field_based,
  2004. int bottom_field,
  2005. int field_select,
  2006. uint8_t **ref_picture,
  2007. h264_chroma_mc_func *pix_op,
  2008. int motion_x, int motion_y,
  2009. int h, int mb_y)
  2010. {
  2011. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2012. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
  2013. ptrdiff_t uvlinesize, linesize;
  2014. const int lowres = s->avctx->lowres;
  2015. const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
  2016. const int block_s = 8>>lowres;
  2017. const int s_mask = (2 << lowres) - 1;
  2018. const int h_edge_pos = s->h_edge_pos >> lowres;
  2019. const int v_edge_pos = s->v_edge_pos >> lowres;
  2020. linesize = s->current_picture.f.linesize[0] << field_based;
  2021. uvlinesize = s->current_picture.f.linesize[1] << field_based;
  2022. // FIXME obviously not perfect but qpel will not work in lowres anyway
  2023. if (s->quarter_sample) {
  2024. motion_x /= 2;
  2025. motion_y /= 2;
  2026. }
  2027. if(field_based){
  2028. motion_y += (bottom_field - field_select)*((1 << lowres)-1);
  2029. }
  2030. sx = motion_x & s_mask;
  2031. sy = motion_y & s_mask;
  2032. src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
  2033. src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
  2034. if (s->out_format == FMT_H263) {
  2035. uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
  2036. uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
  2037. uvsrc_x = src_x >> 1;
  2038. uvsrc_y = src_y >> 1;
  2039. } else if (s->out_format == FMT_H261) {
  2040. // even chroma mv's are full pel in H261
  2041. mx = motion_x / 4;
  2042. my = motion_y / 4;
  2043. uvsx = (2 * mx) & s_mask;
  2044. uvsy = (2 * my) & s_mask;
  2045. uvsrc_x = s->mb_x * block_s + (mx >> lowres);
  2046. uvsrc_y = mb_y * block_s + (my >> lowres);
  2047. } else {
  2048. if(s->chroma_y_shift){
  2049. mx = motion_x / 2;
  2050. my = motion_y / 2;
  2051. uvsx = mx & s_mask;
  2052. uvsy = my & s_mask;
  2053. uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
  2054. uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
  2055. } else {
  2056. if(s->chroma_x_shift){
  2057. //Chroma422
  2058. mx = motion_x / 2;
  2059. uvsx = mx & s_mask;
  2060. uvsy = motion_y & s_mask;
  2061. uvsrc_y = src_y;
  2062. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  2063. } else {
  2064. //Chroma444
  2065. uvsx = motion_x & s_mask;
  2066. uvsy = motion_y & s_mask;
  2067. uvsrc_x = src_x;
  2068. uvsrc_y = src_y;
  2069. }
  2070. }
  2071. }
  2072. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  2073. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  2074. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  2075. if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
  2076. (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  2077. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
  2078. linesize >> field_based, linesize >> field_based,
  2079. 17, 17 + field_based,
  2080. src_x, src_y << field_based, h_edge_pos,
  2081. v_edge_pos);
  2082. ptr_y = s->edge_emu_buffer;
  2083. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  2084. uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
  2085. s->vdsp.emulated_edge_mc(uvbuf, ptr_cb,
  2086. uvlinesize >> field_based, uvlinesize >> field_based,
  2087. 9, 9 + field_based,
  2088. uvsrc_x, uvsrc_y << field_based,
  2089. h_edge_pos >> 1, v_edge_pos >> 1);
  2090. s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr,
  2091. uvlinesize >> field_based,uvlinesize >> field_based,
  2092. 9, 9 + field_based,
  2093. uvsrc_x, uvsrc_y << field_based,
  2094. h_edge_pos >> 1, v_edge_pos >> 1);
  2095. ptr_cb = uvbuf;
  2096. ptr_cr = uvbuf + 16;
  2097. }
  2098. }
  2099. // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
  2100. if (bottom_field) {
  2101. dest_y += s->linesize;
  2102. dest_cb += s->uvlinesize;
  2103. dest_cr += s->uvlinesize;
  2104. }
  2105. if (field_select) {
  2106. ptr_y += s->linesize;
  2107. ptr_cb += s->uvlinesize;
  2108. ptr_cr += s->uvlinesize;
  2109. }
  2110. sx = (sx << 2) >> lowres;
  2111. sy = (sy << 2) >> lowres;
  2112. pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
  2113. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  2114. int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
  2115. uvsx = (uvsx << 2) >> lowres;
  2116. uvsy = (uvsy << 2) >> lowres;
  2117. if (hc) {
  2118. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
  2119. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
  2120. }
  2121. }
  2122. // FIXME h261 lowres loop filter
  2123. }
  2124. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  2125. uint8_t *dest_cb, uint8_t *dest_cr,
  2126. uint8_t **ref_picture,
  2127. h264_chroma_mc_func * pix_op,
  2128. int mx, int my)
  2129. {
  2130. const int lowres = s->avctx->lowres;
  2131. const int op_index = FFMIN(lowres, 3);
  2132. const int block_s = 8 >> lowres;
  2133. const int s_mask = (2 << lowres) - 1;
  2134. const int h_edge_pos = s->h_edge_pos >> lowres + 1;
  2135. const int v_edge_pos = s->v_edge_pos >> lowres + 1;
  2136. int emu = 0, src_x, src_y, sx, sy;
  2137. ptrdiff_t offset;
  2138. uint8_t *ptr;
  2139. if (s->quarter_sample) {
  2140. mx /= 2;
  2141. my /= 2;
  2142. }
  2143. /* In case of 8X8, we construct a single chroma motion vector
  2144. with a special rounding */
  2145. mx = ff_h263_round_chroma(mx);
  2146. my = ff_h263_round_chroma(my);
  2147. sx = mx & s_mask;
  2148. sy = my & s_mask;
  2149. src_x = s->mb_x * block_s + (mx >> lowres + 1);
  2150. src_y = s->mb_y * block_s + (my >> lowres + 1);
  2151. offset = src_y * s->uvlinesize + src_x;
  2152. ptr = ref_picture[1] + offset;
  2153. if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
  2154. (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
  2155. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  2156. s->uvlinesize, s->uvlinesize,
  2157. 9, 9,
  2158. src_x, src_y, h_edge_pos, v_edge_pos);
  2159. ptr = s->edge_emu_buffer;
  2160. emu = 1;
  2161. }
  2162. sx = (sx << 2) >> lowres;
  2163. sy = (sy << 2) >> lowres;
  2164. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  2165. ptr = ref_picture[2] + offset;
  2166. if (emu) {
  2167. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  2168. s->uvlinesize, s->uvlinesize,
  2169. 9, 9,
  2170. src_x, src_y, h_edge_pos, v_edge_pos);
  2171. ptr = s->edge_emu_buffer;
  2172. }
  2173. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  2174. }
  2175. /**
  2176. * motion compensation of a single macroblock
  2177. * @param s context
  2178. * @param dest_y luma destination pointer
  2179. * @param dest_cb chroma cb/u destination pointer
  2180. * @param dest_cr chroma cr/v destination pointer
  2181. * @param dir direction (0->forward, 1->backward)
  2182. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  2183. * @param pix_op halfpel motion compensation function (average or put normally)
  2184. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2185. */
  2186. static inline void MPV_motion_lowres(MpegEncContext *s,
  2187. uint8_t *dest_y, uint8_t *dest_cb,
  2188. uint8_t *dest_cr,
  2189. int dir, uint8_t **ref_picture,
  2190. h264_chroma_mc_func *pix_op)
  2191. {
  2192. int mx, my;
  2193. int mb_x, mb_y, i;
  2194. const int lowres = s->avctx->lowres;
  2195. const int block_s = 8 >>lowres;
  2196. mb_x = s->mb_x;
  2197. mb_y = s->mb_y;
  2198. switch (s->mv_type) {
  2199. case MV_TYPE_16X16:
  2200. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2201. 0, 0, 0,
  2202. ref_picture, pix_op,
  2203. s->mv[dir][0][0], s->mv[dir][0][1],
  2204. 2 * block_s, mb_y);
  2205. break;
  2206. case MV_TYPE_8X8:
  2207. mx = 0;
  2208. my = 0;
  2209. for (i = 0; i < 4; i++) {
  2210. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
  2211. s->linesize) * block_s,
  2212. ref_picture[0], 0, 0,
  2213. (2 * mb_x + (i & 1)) * block_s,
  2214. (2 * mb_y + (i >> 1)) * block_s,
  2215. s->width, s->height, s->linesize,
  2216. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  2217. block_s, block_s, pix_op,
  2218. s->mv[dir][i][0], s->mv[dir][i][1]);
  2219. mx += s->mv[dir][i][0];
  2220. my += s->mv[dir][i][1];
  2221. }
  2222. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
  2223. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
  2224. pix_op, mx, my);
  2225. break;
  2226. case MV_TYPE_FIELD:
  2227. if (s->picture_structure == PICT_FRAME) {
  2228. /* top field */
  2229. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2230. 1, 0, s->field_select[dir][0],
  2231. ref_picture, pix_op,
  2232. s->mv[dir][0][0], s->mv[dir][0][1],
  2233. block_s, mb_y);
  2234. /* bottom field */
  2235. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2236. 1, 1, s->field_select[dir][1],
  2237. ref_picture, pix_op,
  2238. s->mv[dir][1][0], s->mv[dir][1][1],
  2239. block_s, mb_y);
  2240. } else {
  2241. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  2242. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  2243. ref_picture = s->current_picture_ptr->f.data;
  2244. }
  2245. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2246. 0, 0, s->field_select[dir][0],
  2247. ref_picture, pix_op,
  2248. s->mv[dir][0][0],
  2249. s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
  2250. }
  2251. break;
  2252. case MV_TYPE_16X8:
  2253. for (i = 0; i < 2; i++) {
  2254. uint8_t **ref2picture;
  2255. if (s->picture_structure == s->field_select[dir][i] + 1 ||
  2256. s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  2257. ref2picture = ref_picture;
  2258. } else {
  2259. ref2picture = s->current_picture_ptr->f.data;
  2260. }
  2261. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2262. 0, 0, s->field_select[dir][i],
  2263. ref2picture, pix_op,
  2264. s->mv[dir][i][0], s->mv[dir][i][1] +
  2265. 2 * block_s * i, block_s, mb_y >> 1);
  2266. dest_y += 2 * block_s * s->linesize;
  2267. dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2268. dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2269. }
  2270. break;
  2271. case MV_TYPE_DMV:
  2272. if (s->picture_structure == PICT_FRAME) {
  2273. for (i = 0; i < 2; i++) {
  2274. int j;
  2275. for (j = 0; j < 2; j++) {
  2276. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2277. 1, j, j ^ i,
  2278. ref_picture, pix_op,
  2279. s->mv[dir][2 * i + j][0],
  2280. s->mv[dir][2 * i + j][1],
  2281. block_s, mb_y);
  2282. }
  2283. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2284. }
  2285. } else {
  2286. for (i = 0; i < 2; i++) {
  2287. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2288. 0, 0, s->picture_structure != i + 1,
  2289. ref_picture, pix_op,
  2290. s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
  2291. 2 * block_s, mb_y >> 1);
  2292. // after put we make avg of the same block
  2293. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2294. // opposite parity is always in the same
  2295. // frame if this is second field
  2296. if (!s->first_field) {
  2297. ref_picture = s->current_picture_ptr->f.data;
  2298. }
  2299. }
  2300. }
  2301. break;
  2302. default:
  2303. av_assert2(0);
  2304. }
  2305. }
  2306. /**
  2307. * find the lowest MB row referenced in the MVs
  2308. */
  2309. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  2310. {
  2311. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  2312. int my, off, i, mvs;
  2313. if (s->picture_structure != PICT_FRAME || s->mcsel)
  2314. goto unhandled;
  2315. switch (s->mv_type) {
  2316. case MV_TYPE_16X16:
  2317. mvs = 1;
  2318. break;
  2319. case MV_TYPE_16X8:
  2320. mvs = 2;
  2321. break;
  2322. case MV_TYPE_8X8:
  2323. mvs = 4;
  2324. break;
  2325. default:
  2326. goto unhandled;
  2327. }
  2328. for (i = 0; i < mvs; i++) {
  2329. my = s->mv[dir][i][1]<<qpel_shift;
  2330. my_max = FFMAX(my_max, my);
  2331. my_min = FFMIN(my_min, my);
  2332. }
  2333. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  2334. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  2335. unhandled:
  2336. return s->mb_height-1;
  2337. }
  2338. /* put block[] to dest[] */
  2339. static inline void put_dct(MpegEncContext *s,
  2340. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2341. {
  2342. s->dct_unquantize_intra(s, block, i, qscale);
  2343. s->dsp.idct_put (dest, line_size, block);
  2344. }
  2345. /* add block[] to dest[] */
  2346. static inline void add_dct(MpegEncContext *s,
  2347. int16_t *block, int i, uint8_t *dest, int line_size)
  2348. {
  2349. if (s->block_last_index[i] >= 0) {
  2350. s->dsp.idct_add (dest, line_size, block);
  2351. }
  2352. }
  2353. static inline void add_dequant_dct(MpegEncContext *s,
  2354. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2355. {
  2356. if (s->block_last_index[i] >= 0) {
  2357. s->dct_unquantize_inter(s, block, i, qscale);
  2358. s->dsp.idct_add (dest, line_size, block);
  2359. }
  2360. }
  2361. /**
  2362. * Clean dc, ac, coded_block for the current non-intra MB.
  2363. */
  2364. void ff_clean_intra_table_entries(MpegEncContext *s)
  2365. {
  2366. int wrap = s->b8_stride;
  2367. int xy = s->block_index[0];
  2368. s->dc_val[0][xy ] =
  2369. s->dc_val[0][xy + 1 ] =
  2370. s->dc_val[0][xy + wrap] =
  2371. s->dc_val[0][xy + 1 + wrap] = 1024;
  2372. /* ac pred */
  2373. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2374. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2375. if (s->msmpeg4_version>=3) {
  2376. s->coded_block[xy ] =
  2377. s->coded_block[xy + 1 ] =
  2378. s->coded_block[xy + wrap] =
  2379. s->coded_block[xy + 1 + wrap] = 0;
  2380. }
  2381. /* chroma */
  2382. wrap = s->mb_stride;
  2383. xy = s->mb_x + s->mb_y * wrap;
  2384. s->dc_val[1][xy] =
  2385. s->dc_val[2][xy] = 1024;
  2386. /* ac pred */
  2387. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2388. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2389. s->mbintra_table[xy]= 0;
  2390. }
  2391. /* generic function called after a macroblock has been parsed by the
  2392. decoder or after it has been encoded by the encoder.
  2393. Important variables used:
  2394. s->mb_intra : true if intra macroblock
  2395. s->mv_dir : motion vector direction
  2396. s->mv_type : motion vector type
  2397. s->mv : motion vector
  2398. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2399. */
  2400. static av_always_inline
  2401. void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  2402. int lowres_flag, int is_mpeg12)
  2403. {
  2404. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2405. if (CONFIG_XVMC &&
  2406. s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
  2407. s->avctx->hwaccel->decode_mb(s);//xvmc uses pblocks
  2408. return;
  2409. }
  2410. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  2411. /* print DCT coefficients */
  2412. int i,j;
  2413. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  2414. for(i=0; i<6; i++){
  2415. for(j=0; j<64; j++){
  2416. av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
  2417. }
  2418. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2419. }
  2420. }
  2421. s->current_picture.qscale_table[mb_xy] = s->qscale;
  2422. /* update DC predictors for P macroblocks */
  2423. if (!s->mb_intra) {
  2424. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  2425. if(s->mbintra_table[mb_xy])
  2426. ff_clean_intra_table_entries(s);
  2427. } else {
  2428. s->last_dc[0] =
  2429. s->last_dc[1] =
  2430. s->last_dc[2] = 128 << s->intra_dc_precision;
  2431. }
  2432. }
  2433. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  2434. s->mbintra_table[mb_xy]=1;
  2435. if ( (s->flags&CODEC_FLAG_PSNR)
  2436. || s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor
  2437. || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
  2438. uint8_t *dest_y, *dest_cb, *dest_cr;
  2439. int dct_linesize, dct_offset;
  2440. op_pixels_func (*op_pix)[4];
  2441. qpel_mc_func (*op_qpix)[16];
  2442. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2443. const int uvlinesize = s->current_picture.f.linesize[1];
  2444. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  2445. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  2446. /* avoid copy if macroblock skipped in last frame too */
  2447. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2448. if(!s->encoding){
  2449. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2450. if (s->mb_skipped) {
  2451. s->mb_skipped= 0;
  2452. av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
  2453. *mbskip_ptr = 1;
  2454. } else if(!s->current_picture.reference) {
  2455. *mbskip_ptr = 1;
  2456. } else{
  2457. *mbskip_ptr = 0; /* not skipped */
  2458. }
  2459. }
  2460. dct_linesize = linesize << s->interlaced_dct;
  2461. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  2462. if(readable){
  2463. dest_y= s->dest[0];
  2464. dest_cb= s->dest[1];
  2465. dest_cr= s->dest[2];
  2466. }else{
  2467. dest_y = s->b_scratchpad;
  2468. dest_cb= s->b_scratchpad+16*linesize;
  2469. dest_cr= s->b_scratchpad+32*linesize;
  2470. }
  2471. if (!s->mb_intra) {
  2472. /* motion handling */
  2473. /* decoding or more than one mb_type (MC was already done otherwise) */
  2474. if(!s->encoding){
  2475. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  2476. if (s->mv_dir & MV_DIR_FORWARD) {
  2477. ff_thread_await_progress(&s->last_picture_ptr->tf,
  2478. ff_MPV_lowest_referenced_row(s, 0),
  2479. 0);
  2480. }
  2481. if (s->mv_dir & MV_DIR_BACKWARD) {
  2482. ff_thread_await_progress(&s->next_picture_ptr->tf,
  2483. ff_MPV_lowest_referenced_row(s, 1),
  2484. 0);
  2485. }
  2486. }
  2487. if(lowres_flag){
  2488. h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
  2489. if (s->mv_dir & MV_DIR_FORWARD) {
  2490. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
  2491. op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
  2492. }
  2493. if (s->mv_dir & MV_DIR_BACKWARD) {
  2494. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
  2495. }
  2496. }else{
  2497. op_qpix = s->me.qpel_put;
  2498. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  2499. op_pix = s->hdsp.put_pixels_tab;
  2500. }else{
  2501. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  2502. }
  2503. if (s->mv_dir & MV_DIR_FORWARD) {
  2504. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  2505. op_pix = s->hdsp.avg_pixels_tab;
  2506. op_qpix= s->me.qpel_avg;
  2507. }
  2508. if (s->mv_dir & MV_DIR_BACKWARD) {
  2509. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  2510. }
  2511. }
  2512. }
  2513. /* skip dequant / idct if we are really late ;) */
  2514. if(s->avctx->skip_idct){
  2515. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  2516. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  2517. || s->avctx->skip_idct >= AVDISCARD_ALL)
  2518. goto skip_idct;
  2519. }
  2520. /* add dct residue */
  2521. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  2522. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2523. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2524. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2525. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2526. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2527. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2528. if (s->chroma_y_shift){
  2529. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2530. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2531. }else{
  2532. dct_linesize >>= 1;
  2533. dct_offset >>=1;
  2534. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2535. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2536. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2537. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2538. }
  2539. }
  2540. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  2541. add_dct(s, block[0], 0, dest_y , dct_linesize);
  2542. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  2543. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  2544. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  2545. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2546. if(s->chroma_y_shift){//Chroma420
  2547. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2548. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2549. }else{
  2550. //chroma422
  2551. dct_linesize = uvlinesize << s->interlaced_dct;
  2552. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2553. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2554. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2555. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2556. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2557. if(!s->chroma_x_shift){//Chroma444
  2558. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  2559. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  2560. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  2561. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  2562. }
  2563. }
  2564. }//fi gray
  2565. }
  2566. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  2567. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2568. }
  2569. } else {
  2570. /* dct only in intra block */
  2571. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  2572. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2573. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2574. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2575. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2576. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2577. if(s->chroma_y_shift){
  2578. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2579. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2580. }else{
  2581. dct_offset >>=1;
  2582. dct_linesize >>=1;
  2583. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2584. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2585. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2586. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2587. }
  2588. }
  2589. }else{
  2590. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2591. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2592. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2593. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2594. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2595. if(s->chroma_y_shift){
  2596. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2597. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2598. }else{
  2599. dct_linesize = uvlinesize << s->interlaced_dct;
  2600. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2601. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  2602. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  2603. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2604. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2605. if(!s->chroma_x_shift){//Chroma444
  2606. s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  2607. s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  2608. s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  2609. s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  2610. }
  2611. }
  2612. }//gray
  2613. }
  2614. }
  2615. skip_idct:
  2616. if(!readable){
  2617. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2618. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2619. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2620. }
  2621. }
  2622. }
  2623. void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
  2624. #if !CONFIG_SMALL
  2625. if(s->out_format == FMT_MPEG1) {
  2626. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  2627. else MPV_decode_mb_internal(s, block, 0, 1);
  2628. } else
  2629. #endif
  2630. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  2631. else MPV_decode_mb_internal(s, block, 0, 0);
  2632. }
  2633. /**
  2634. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2635. */
  2636. void ff_draw_horiz_band(AVCodecContext *avctx, DSPContext *dsp, Picture *cur,
  2637. Picture *last, int y, int h, int picture_structure,
  2638. int first_field, int low_delay,
  2639. int v_edge_pos, int h_edge_pos)
  2640. {
  2641. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  2642. int vshift = desc->log2_chroma_h;
  2643. const int field_pic = picture_structure != PICT_FRAME;
  2644. if(field_pic){
  2645. h <<= 1;
  2646. y <<= 1;
  2647. }
  2648. h = FFMIN(h, avctx->height - y);
  2649. if(field_pic && first_field && !(avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2650. if (avctx->draw_horiz_band) {
  2651. AVFrame *src;
  2652. int offset[AV_NUM_DATA_POINTERS];
  2653. int i;
  2654. if(cur->f.pict_type == AV_PICTURE_TYPE_B || low_delay ||
  2655. (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2656. src = &cur->f;
  2657. else if (last)
  2658. src = &last->f;
  2659. else
  2660. return;
  2661. if (cur->f.pict_type == AV_PICTURE_TYPE_B &&
  2662. picture_structure == PICT_FRAME &&
  2663. avctx->codec_id != AV_CODEC_ID_SVQ3) {
  2664. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  2665. offset[i] = 0;
  2666. }else{
  2667. offset[0]= y * src->linesize[0];
  2668. offset[1]=
  2669. offset[2]= (y >> vshift) * src->linesize[1];
  2670. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  2671. offset[i] = 0;
  2672. }
  2673. emms_c();
  2674. avctx->draw_horiz_band(avctx, src, offset,
  2675. y, picture_structure, h);
  2676. }
  2677. }
  2678. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  2679. {
  2680. ff_draw_horiz_band(s->avctx, &s->dsp, s->current_picture_ptr,
  2681. s->last_picture_ptr, y, h, s->picture_structure,
  2682. s->first_field, s->low_delay,
  2683. s->v_edge_pos, s->h_edge_pos);
  2684. }
  2685. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2686. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2687. const int uvlinesize = s->current_picture.f.linesize[1];
  2688. const int mb_size= 4 - s->avctx->lowres;
  2689. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2690. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2691. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2692. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2693. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2694. 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;
  2695. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2696. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2697. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2698. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2699. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2700. {
  2701. if(s->picture_structure==PICT_FRAME){
  2702. s->dest[0] += s->mb_y * linesize << mb_size;
  2703. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2704. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2705. }else{
  2706. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2707. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2708. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2709. av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2710. }
  2711. }
  2712. }
  2713. /**
  2714. * Permute an 8x8 block.
  2715. * @param block the block which will be permuted according to the given permutation vector
  2716. * @param permutation the permutation vector
  2717. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2718. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2719. * (inverse) permutated to scantable order!
  2720. */
  2721. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2722. {
  2723. int i;
  2724. int16_t temp[64];
  2725. if(last<=0) return;
  2726. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2727. for(i=0; i<=last; i++){
  2728. const int j= scantable[i];
  2729. temp[j]= block[j];
  2730. block[j]=0;
  2731. }
  2732. for(i=0; i<=last; i++){
  2733. const int j= scantable[i];
  2734. const int perm_j= permutation[j];
  2735. block[perm_j]= temp[j];
  2736. }
  2737. }
  2738. void ff_mpeg_flush(AVCodecContext *avctx){
  2739. int i;
  2740. MpegEncContext *s = avctx->priv_data;
  2741. if(s==NULL || s->picture==NULL)
  2742. return;
  2743. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2744. ff_mpeg_unref_picture(s, &s->picture[i]);
  2745. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2746. ff_mpeg_unref_picture(s, &s->current_picture);
  2747. ff_mpeg_unref_picture(s, &s->last_picture);
  2748. ff_mpeg_unref_picture(s, &s->next_picture);
  2749. s->mb_x= s->mb_y= 0;
  2750. s->closed_gop= 0;
  2751. s->parse_context.state= -1;
  2752. s->parse_context.frame_start_found= 0;
  2753. s->parse_context.overread= 0;
  2754. s->parse_context.overread_index= 0;
  2755. s->parse_context.index= 0;
  2756. s->parse_context.last_index= 0;
  2757. s->bitstream_buffer_size=0;
  2758. s->pp_time=0;
  2759. }
  2760. /**
  2761. * set qscale and update qscale dependent variables.
  2762. */
  2763. void ff_set_qscale(MpegEncContext * s, int qscale)
  2764. {
  2765. if (qscale < 1)
  2766. qscale = 1;
  2767. else if (qscale > 31)
  2768. qscale = 31;
  2769. s->qscale = qscale;
  2770. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2771. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2772. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2773. }
  2774. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2775. {
  2776. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2777. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2778. }
  2779. #if CONFIG_ERROR_RESILIENCE
  2780. void ff_mpeg_er_frame_start(MpegEncContext *s)
  2781. {
  2782. ERContext *er = &s->er;
  2783. er->cur_pic = s->current_picture_ptr;
  2784. er->last_pic = s->last_picture_ptr;
  2785. er->next_pic = s->next_picture_ptr;
  2786. er->pp_time = s->pp_time;
  2787. er->pb_time = s->pb_time;
  2788. er->quarter_sample = s->quarter_sample;
  2789. er->partitioned_frame = s->partitioned_frame;
  2790. ff_er_frame_start(er);
  2791. }
  2792. #endif /* CONFIG_ERROR_RESILIENCE */