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.

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