2
0

mpegpicture.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Mpeg video formats-related picture management functions
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdint.h>
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "libavutil/imgutils.h"
  25. #include "avcodec.h"
  26. #include "motion_est.h"
  27. #include "mpegpicture.h"
  28. #include "mpegutils.h"
  29. static int make_tables_writable(Picture *pic)
  30. {
  31. int ret, i;
  32. #define MAKE_WRITABLE(table) \
  33. do {\
  34. if (pic->table &&\
  35. (ret = av_buffer_make_writable(&pic->table)) < 0)\
  36. return ret;\
  37. } while (0)
  38. MAKE_WRITABLE(mb_var_buf);
  39. MAKE_WRITABLE(mc_mb_var_buf);
  40. MAKE_WRITABLE(mb_mean_buf);
  41. MAKE_WRITABLE(mbskip_table_buf);
  42. MAKE_WRITABLE(qscale_table_buf);
  43. MAKE_WRITABLE(mb_type_buf);
  44. for (i = 0; i < 2; i++) {
  45. MAKE_WRITABLE(motion_val_buf[i]);
  46. MAKE_WRITABLE(ref_index_buf[i]);
  47. }
  48. return 0;
  49. }
  50. int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
  51. ScratchpadContext *sc, int linesize)
  52. {
  53. # define EMU_EDGE_HEIGHT (4 * 70)
  54. int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
  55. if (avctx->hwaccel)
  56. return 0;
  57. if (linesize < 24) {
  58. av_log(avctx, AV_LOG_ERROR, "Image too small, temporary buffers cannot function\n");
  59. return AVERROR_PATCHWELCOME;
  60. }
  61. if (av_image_check_size2(alloc_size, EMU_EDGE_HEIGHT, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)
  62. return AVERROR(ENOMEM);
  63. // edge emu needs blocksize + filter length - 1
  64. // (= 17x17 for halfpel / 21x21 for H.264)
  65. // VC-1 computes luma and chroma simultaneously and needs 19X19 + 9x9
  66. // at uvlinesize. It supports only YUV420 so 24x24 is enough
  67. // linesize * interlaced * MBsize
  68. // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines
  69. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, sc->edge_emu_buffer, alloc_size, EMU_EDGE_HEIGHT,
  70. fail);
  71. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, me->scratchpad, alloc_size, 4 * 16 * 2,
  72. fail)
  73. me->temp = me->scratchpad;
  74. sc->rd_scratchpad = me->scratchpad;
  75. sc->b_scratchpad = me->scratchpad;
  76. sc->obmc_scratchpad = me->scratchpad + 16;
  77. return 0;
  78. fail:
  79. av_freep(&sc->edge_emu_buffer);
  80. return AVERROR(ENOMEM);
  81. }
  82. /**
  83. * Allocate a frame buffer
  84. */
  85. static int alloc_frame_buffer(AVCodecContext *avctx, Picture *pic,
  86. MotionEstContext *me, ScratchpadContext *sc,
  87. int chroma_x_shift, int chroma_y_shift,
  88. int linesize, int uvlinesize)
  89. {
  90. int edges_needed = av_codec_is_encoder(avctx->codec);
  91. int r, ret;
  92. pic->tf.f = pic->f;
  93. if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  94. avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
  95. avctx->codec_id != AV_CODEC_ID_MSS2) {
  96. if (edges_needed) {
  97. pic->f->width = avctx->width + 2 * EDGE_WIDTH;
  98. pic->f->height = avctx->height + 2 * EDGE_WIDTH;
  99. }
  100. r = ff_thread_get_buffer(avctx, &pic->tf,
  101. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  102. } else {
  103. pic->f->width = avctx->width;
  104. pic->f->height = avctx->height;
  105. pic->f->format = avctx->pix_fmt;
  106. r = avcodec_default_get_buffer2(avctx, pic->f, 0);
  107. }
  108. if (r < 0 || !pic->f->buf[0]) {
  109. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
  110. r, pic->f->data[0]);
  111. return -1;
  112. }
  113. if (edges_needed) {
  114. int i;
  115. for (i = 0; pic->f->data[i]; i++) {
  116. int offset = (EDGE_WIDTH >> (i ? chroma_y_shift : 0)) *
  117. pic->f->linesize[i] +
  118. (EDGE_WIDTH >> (i ? chroma_x_shift : 0));
  119. pic->f->data[i] += offset;
  120. }
  121. pic->f->width = avctx->width;
  122. pic->f->height = avctx->height;
  123. }
  124. if (avctx->hwaccel) {
  125. assert(!pic->hwaccel_picture_private);
  126. if (avctx->hwaccel->frame_priv_data_size) {
  127. pic->hwaccel_priv_buf = av_buffer_allocz(avctx->hwaccel->frame_priv_data_size);
  128. if (!pic->hwaccel_priv_buf) {
  129. av_log(avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  130. return -1;
  131. }
  132. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  133. }
  134. }
  135. if ((linesize && linesize != pic->f->linesize[0]) ||
  136. (uvlinesize && uvlinesize != pic->f->linesize[1])) {
  137. av_log(avctx, AV_LOG_ERROR,
  138. "get_buffer() failed (stride changed: linesize=%d/%d uvlinesize=%d/%d)\n",
  139. linesize, pic->f->linesize[0],
  140. uvlinesize, pic->f->linesize[1]);
  141. ff_mpeg_unref_picture(avctx, pic);
  142. return -1;
  143. }
  144. if (av_pix_fmt_count_planes(pic->f->format) > 2 &&
  145. pic->f->linesize[1] != pic->f->linesize[2]) {
  146. av_log(avctx, AV_LOG_ERROR,
  147. "get_buffer() failed (uv stride mismatch)\n");
  148. ff_mpeg_unref_picture(avctx, pic);
  149. return -1;
  150. }
  151. if (!sc->edge_emu_buffer &&
  152. (ret = ff_mpeg_framesize_alloc(avctx, me, sc,
  153. pic->f->linesize[0])) < 0) {
  154. av_log(avctx, AV_LOG_ERROR,
  155. "get_buffer() failed to allocate context scratch buffers.\n");
  156. ff_mpeg_unref_picture(avctx, pic);
  157. return ret;
  158. }
  159. return 0;
  160. }
  161. static int alloc_picture_tables(AVCodecContext *avctx, Picture *pic, int encoding, int out_format,
  162. int mb_stride, int mb_width, int mb_height, int b8_stride)
  163. {
  164. const int big_mb_num = mb_stride * (mb_height + 1) + 1;
  165. const int mb_array_size = mb_stride * mb_height;
  166. const int b8_array_size = b8_stride * mb_height * 2;
  167. int i;
  168. pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
  169. pic->qscale_table_buf = av_buffer_allocz(big_mb_num + mb_stride);
  170. pic->mb_type_buf = av_buffer_allocz((big_mb_num + mb_stride) *
  171. sizeof(uint32_t));
  172. if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
  173. return AVERROR(ENOMEM);
  174. if (encoding) {
  175. pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  176. pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  177. pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
  178. if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
  179. return AVERROR(ENOMEM);
  180. }
  181. if (out_format == FMT_H263 || encoding ||
  182. #if FF_API_DEBUG_MV
  183. avctx->debug_mv ||
  184. #endif
  185. (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS)) {
  186. int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
  187. int ref_index_size = 4 * mb_array_size;
  188. for (i = 0; mv_size && i < 2; i++) {
  189. pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
  190. pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
  191. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  192. return AVERROR(ENOMEM);
  193. }
  194. }
  195. pic->alloc_mb_width = mb_width;
  196. pic->alloc_mb_height = mb_height;
  197. return 0;
  198. }
  199. /**
  200. * Allocate a Picture.
  201. * The pixels are allocated/set by calling get_buffer() if shared = 0
  202. */
  203. int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me,
  204. ScratchpadContext *sc, int shared, int encoding,
  205. int chroma_x_shift, int chroma_y_shift, int out_format,
  206. int mb_stride, int mb_width, int mb_height, int b8_stride,
  207. ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
  208. {
  209. int i, ret;
  210. if (pic->qscale_table_buf)
  211. if ( pic->alloc_mb_width != mb_width
  212. || pic->alloc_mb_height != mb_height)
  213. ff_free_picture_tables(pic);
  214. if (shared) {
  215. av_assert0(pic->f->data[0]);
  216. pic->shared = 1;
  217. } else {
  218. av_assert0(!pic->f->buf[0]);
  219. if (alloc_frame_buffer(avctx, pic, me, sc,
  220. chroma_x_shift, chroma_y_shift,
  221. *linesize, *uvlinesize) < 0)
  222. return -1;
  223. *linesize = pic->f->linesize[0];
  224. *uvlinesize = pic->f->linesize[1];
  225. }
  226. if (!pic->qscale_table_buf)
  227. ret = alloc_picture_tables(avctx, pic, encoding, out_format,
  228. mb_stride, mb_width, mb_height, b8_stride);
  229. else
  230. ret = make_tables_writable(pic);
  231. if (ret < 0)
  232. goto fail;
  233. if (encoding) {
  234. pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
  235. pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
  236. pic->mb_mean = pic->mb_mean_buf->data;
  237. }
  238. pic->mbskip_table = pic->mbskip_table_buf->data;
  239. pic->qscale_table = pic->qscale_table_buf->data + 2 * mb_stride + 1;
  240. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * mb_stride + 1;
  241. if (pic->motion_val_buf[0]) {
  242. for (i = 0; i < 2; i++) {
  243. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  244. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  245. }
  246. }
  247. return 0;
  248. fail:
  249. av_log(avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
  250. ff_mpeg_unref_picture(avctx, pic);
  251. ff_free_picture_tables(pic);
  252. return AVERROR(ENOMEM);
  253. }
  254. /**
  255. * Deallocate a picture.
  256. */
  257. void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
  258. {
  259. int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
  260. pic->tf.f = pic->f;
  261. /* WM Image / Screen codecs allocate internal buffers with different
  262. * dimensions / colorspaces; ignore user-defined callbacks for these. */
  263. if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  264. avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
  265. avctx->codec_id != AV_CODEC_ID_MSS2)
  266. ff_thread_release_buffer(avctx, &pic->tf);
  267. else if (pic->f)
  268. av_frame_unref(pic->f);
  269. av_buffer_unref(&pic->hwaccel_priv_buf);
  270. if (pic->needs_realloc)
  271. ff_free_picture_tables(pic);
  272. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  273. }
  274. int ff_update_picture_tables(Picture *dst, Picture *src)
  275. {
  276. int i;
  277. #define UPDATE_TABLE(table) \
  278. do { \
  279. if (src->table && \
  280. (!dst->table || dst->table->buffer != src->table->buffer)) { \
  281. av_buffer_unref(&dst->table); \
  282. dst->table = av_buffer_ref(src->table); \
  283. if (!dst->table) { \
  284. ff_free_picture_tables(dst); \
  285. return AVERROR(ENOMEM); \
  286. } \
  287. } \
  288. } while (0)
  289. UPDATE_TABLE(mb_var_buf);
  290. UPDATE_TABLE(mc_mb_var_buf);
  291. UPDATE_TABLE(mb_mean_buf);
  292. UPDATE_TABLE(mbskip_table_buf);
  293. UPDATE_TABLE(qscale_table_buf);
  294. UPDATE_TABLE(mb_type_buf);
  295. for (i = 0; i < 2; i++) {
  296. UPDATE_TABLE(motion_val_buf[i]);
  297. UPDATE_TABLE(ref_index_buf[i]);
  298. }
  299. dst->mb_var = src->mb_var;
  300. dst->mc_mb_var = src->mc_mb_var;
  301. dst->mb_mean = src->mb_mean;
  302. dst->mbskip_table = src->mbskip_table;
  303. dst->qscale_table = src->qscale_table;
  304. dst->mb_type = src->mb_type;
  305. for (i = 0; i < 2; i++) {
  306. dst->motion_val[i] = src->motion_val[i];
  307. dst->ref_index[i] = src->ref_index[i];
  308. }
  309. dst->alloc_mb_width = src->alloc_mb_width;
  310. dst->alloc_mb_height = src->alloc_mb_height;
  311. return 0;
  312. }
  313. int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
  314. {
  315. int ret;
  316. av_assert0(!dst->f->buf[0]);
  317. av_assert0(src->f->buf[0]);
  318. src->tf.f = src->f;
  319. dst->tf.f = dst->f;
  320. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  321. if (ret < 0)
  322. goto fail;
  323. ret = ff_update_picture_tables(dst, src);
  324. if (ret < 0)
  325. goto fail;
  326. if (src->hwaccel_picture_private) {
  327. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  328. if (!dst->hwaccel_priv_buf) {
  329. ret = AVERROR(ENOMEM);
  330. goto fail;
  331. }
  332. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  333. }
  334. dst->field_picture = src->field_picture;
  335. dst->mb_var_sum = src->mb_var_sum;
  336. dst->mc_mb_var_sum = src->mc_mb_var_sum;
  337. dst->b_frame_score = src->b_frame_score;
  338. dst->needs_realloc = src->needs_realloc;
  339. dst->reference = src->reference;
  340. dst->shared = src->shared;
  341. memcpy(dst->encoding_error, src->encoding_error,
  342. sizeof(dst->encoding_error));
  343. return 0;
  344. fail:
  345. ff_mpeg_unref_picture(avctx, dst);
  346. return ret;
  347. }
  348. static inline int pic_is_unused(Picture *pic)
  349. {
  350. if (!pic->f->buf[0])
  351. return 1;
  352. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  353. return 1;
  354. return 0;
  355. }
  356. static int find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
  357. {
  358. int i;
  359. if (shared) {
  360. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  361. if (!picture[i].f->buf[0])
  362. return i;
  363. }
  364. } else {
  365. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  366. if (pic_is_unused(&picture[i]))
  367. return i;
  368. }
  369. }
  370. av_log(avctx, AV_LOG_FATAL,
  371. "Internal error, picture buffer overflow\n");
  372. /* We could return -1, but the codec would crash trying to draw into a
  373. * non-existing frame anyway. This is safer than waiting for a random crash.
  374. * Also the return of this is never useful, an encoder must only allocate
  375. * as much as allowed in the specification. This has no relationship to how
  376. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  377. * enough for such valid streams).
  378. * Plus, a decoder has to check stream validity and remove frames if too
  379. * many reference frames are around. Waiting for "OOM" is not correct at
  380. * all. Similarly, missing reference frames have to be replaced by
  381. * interpolated/MC frames, anything else is a bug in the codec ...
  382. */
  383. abort();
  384. return -1;
  385. }
  386. int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
  387. {
  388. int ret = find_unused_picture(avctx, picture, shared);
  389. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  390. if (picture[ret].needs_realloc) {
  391. picture[ret].needs_realloc = 0;
  392. ff_free_picture_tables(&picture[ret]);
  393. ff_mpeg_unref_picture(avctx, &picture[ret]);
  394. }
  395. }
  396. return ret;
  397. }
  398. void ff_free_picture_tables(Picture *pic)
  399. {
  400. int i;
  401. pic->alloc_mb_width =
  402. pic->alloc_mb_height = 0;
  403. av_buffer_unref(&pic->mb_var_buf);
  404. av_buffer_unref(&pic->mc_mb_var_buf);
  405. av_buffer_unref(&pic->mb_mean_buf);
  406. av_buffer_unref(&pic->mbskip_table_buf);
  407. av_buffer_unref(&pic->qscale_table_buf);
  408. av_buffer_unref(&pic->mb_type_buf);
  409. for (i = 0; i < 2; i++) {
  410. av_buffer_unref(&pic->motion_val_buf[i]);
  411. av_buffer_unref(&pic->ref_index_buf[i]);
  412. }
  413. }