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.

428 lines
14KB

  1. /*
  2. * Mpeg video formats-related picture management functions
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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 "avcodec.h"
  24. #include "motion_est.h"
  25. #include "mpegpicture.h"
  26. #include "mpegutils.h"
  27. static int make_tables_writable(Picture *pic)
  28. {
  29. int ret, i;
  30. #define MAKE_WRITABLE(table) \
  31. do {\
  32. if (pic->table &&\
  33. (ret = av_buffer_make_writable(&pic->table)) < 0)\
  34. return ret;\
  35. } while (0)
  36. MAKE_WRITABLE(mb_var_buf);
  37. MAKE_WRITABLE(mc_mb_var_buf);
  38. MAKE_WRITABLE(mb_mean_buf);
  39. MAKE_WRITABLE(mbskip_table_buf);
  40. MAKE_WRITABLE(qscale_table_buf);
  41. MAKE_WRITABLE(mb_type_buf);
  42. for (i = 0; i < 2; i++) {
  43. MAKE_WRITABLE(motion_val_buf[i]);
  44. MAKE_WRITABLE(ref_index_buf[i]);
  45. }
  46. return 0;
  47. }
  48. int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
  49. ScratchpadContext *sc, int linesize)
  50. {
  51. int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
  52. // edge emu needs blocksize + filter length - 1
  53. // (= 17x17 for halfpel / 21x21 for H.264)
  54. // VC-1 computes luma and chroma simultaneously and needs 19X19 + 9x9
  55. // at uvlinesize. It supports only YUV420 so 24x24 is enough
  56. // linesize * interlaced * MBsize
  57. FF_ALLOCZ_OR_GOTO(avctx, sc->edge_emu_buffer, alloc_size * 2 * 24,
  58. fail);
  59. FF_ALLOCZ_OR_GOTO(avctx, me->scratchpad, alloc_size * 2 * 16 * 3,
  60. fail)
  61. me->temp = me->scratchpad;
  62. sc->rd_scratchpad = me->scratchpad;
  63. sc->b_scratchpad = me->scratchpad;
  64. sc->obmc_scratchpad = me->scratchpad + 16;
  65. return 0;
  66. fail:
  67. av_freep(&sc->edge_emu_buffer);
  68. return AVERROR(ENOMEM);
  69. }
  70. /**
  71. * Allocate a frame buffer
  72. */
  73. static int alloc_frame_buffer(AVCodecContext *avctx, Picture *pic,
  74. MotionEstContext *me, ScratchpadContext *sc,
  75. int chroma_x_shift, int chroma_y_shift,
  76. int linesize, int uvlinesize)
  77. {
  78. int edges_needed = av_codec_is_encoder(avctx->codec);
  79. int r, ret;
  80. pic->tf.f = pic->f;
  81. if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  82. avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
  83. avctx->codec_id != AV_CODEC_ID_MSS2) {
  84. if (edges_needed) {
  85. pic->f->width = avctx->width + 2 * EDGE_WIDTH;
  86. pic->f->height = avctx->height + 2 * EDGE_WIDTH;
  87. }
  88. r = ff_thread_get_buffer(avctx, &pic->tf,
  89. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  90. } else {
  91. pic->f->width = avctx->width;
  92. pic->f->height = avctx->height;
  93. pic->f->format = avctx->pix_fmt;
  94. r = avcodec_default_get_buffer2(avctx, pic->f, 0);
  95. }
  96. if (r < 0 || !pic->f->buf[0]) {
  97. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
  98. r, pic->f->data[0]);
  99. return -1;
  100. }
  101. if (edges_needed) {
  102. int i;
  103. for (i = 0; pic->f->data[i]; i++) {
  104. int offset = (EDGE_WIDTH >> (i ? chroma_y_shift : 0)) *
  105. pic->f->linesize[i] +
  106. (EDGE_WIDTH >> (i ? chroma_x_shift : 0));
  107. pic->f->data[i] += offset;
  108. }
  109. pic->f->width = avctx->width;
  110. pic->f->height = avctx->height;
  111. }
  112. if (avctx->hwaccel) {
  113. assert(!pic->hwaccel_picture_private);
  114. if (avctx->hwaccel->frame_priv_data_size) {
  115. pic->hwaccel_priv_buf = av_buffer_allocz(avctx->hwaccel->frame_priv_data_size);
  116. if (!pic->hwaccel_priv_buf) {
  117. av_log(avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  118. return -1;
  119. }
  120. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  121. }
  122. }
  123. if (linesize && (linesize != pic->f->linesize[0] ||
  124. uvlinesize != pic->f->linesize[1])) {
  125. av_log(avctx, AV_LOG_ERROR,
  126. "get_buffer() failed (stride changed)\n");
  127. ff_mpeg_unref_picture(avctx, pic);
  128. return -1;
  129. }
  130. if (pic->f->linesize[1] != pic->f->linesize[2]) {
  131. av_log(avctx, AV_LOG_ERROR,
  132. "get_buffer() failed (uv stride mismatch)\n");
  133. ff_mpeg_unref_picture(avctx, pic);
  134. return -1;
  135. }
  136. if (!sc->edge_emu_buffer &&
  137. (ret = ff_mpeg_framesize_alloc(avctx, me, sc,
  138. pic->f->linesize[0])) < 0) {
  139. av_log(avctx, AV_LOG_ERROR,
  140. "get_buffer() failed to allocate context scratch buffers.\n");
  141. ff_mpeg_unref_picture(avctx, pic);
  142. return ret;
  143. }
  144. return 0;
  145. }
  146. static int alloc_picture_tables(Picture *pic, int encoding, int out_format,
  147. int mb_stride, int mb_height, int b8_stride)
  148. {
  149. const int big_mb_num = mb_stride * (mb_height + 1) + 1;
  150. const int mb_array_size = mb_stride * mb_height;
  151. const int b8_array_size = b8_stride * mb_height * 2;
  152. int i;
  153. pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
  154. pic->qscale_table_buf = av_buffer_allocz(big_mb_num + mb_stride);
  155. pic->mb_type_buf = av_buffer_allocz((big_mb_num + mb_stride) *
  156. sizeof(uint32_t));
  157. if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
  158. return AVERROR(ENOMEM);
  159. if (encoding) {
  160. pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  161. pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  162. pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
  163. if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
  164. return AVERROR(ENOMEM);
  165. }
  166. if (out_format == FMT_H263 || encoding) {
  167. int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
  168. int ref_index_size = 4 * mb_array_size;
  169. for (i = 0; mv_size && i < 2; i++) {
  170. pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
  171. pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
  172. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  173. return AVERROR(ENOMEM);
  174. }
  175. }
  176. return 0;
  177. }
  178. /**
  179. * Allocate a Picture.
  180. * The pixels are allocated/set by calling get_buffer() if shared = 0
  181. */
  182. int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me,
  183. ScratchpadContext *sc, int shared, int encoding,
  184. int chroma_x_shift, int chroma_y_shift, int out_format,
  185. int mb_stride, int mb_height, int b8_stride,
  186. ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
  187. {
  188. int i, ret;
  189. if (shared) {
  190. assert(pic->f->data[0]);
  191. pic->shared = 1;
  192. } else {
  193. assert(!pic->f->buf[0]);
  194. if (alloc_frame_buffer(avctx, pic, me, sc,
  195. chroma_x_shift, chroma_y_shift,
  196. *linesize, *uvlinesize) < 0)
  197. return -1;
  198. *linesize = pic->f->linesize[0];
  199. *uvlinesize = pic->f->linesize[1];
  200. }
  201. if (!pic->qscale_table_buf)
  202. ret = alloc_picture_tables(pic, encoding, out_format,
  203. mb_stride, mb_height, b8_stride);
  204. else
  205. ret = make_tables_writable(pic);
  206. if (ret < 0)
  207. goto fail;
  208. if (encoding) {
  209. pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
  210. pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
  211. pic->mb_mean = pic->mb_mean_buf->data;
  212. }
  213. pic->mbskip_table = pic->mbskip_table_buf->data;
  214. pic->qscale_table = pic->qscale_table_buf->data + 2 * mb_stride + 1;
  215. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * mb_stride + 1;
  216. if (pic->motion_val_buf[0]) {
  217. for (i = 0; i < 2; i++) {
  218. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  219. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  220. }
  221. }
  222. return 0;
  223. fail:
  224. av_log(avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
  225. ff_mpeg_unref_picture(avctx, pic);
  226. ff_free_picture_tables(pic);
  227. return AVERROR(ENOMEM);
  228. }
  229. /**
  230. * Deallocate a picture.
  231. */
  232. void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
  233. {
  234. pic->tf.f = pic->f;
  235. /* WM Image / Screen codecs allocate internal buffers with different
  236. * dimensions / colorspaces; ignore user-defined callbacks for these. */
  237. if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  238. avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
  239. avctx->codec_id != AV_CODEC_ID_MSS2)
  240. ff_thread_release_buffer(avctx, &pic->tf);
  241. else if (pic->f)
  242. av_frame_unref(pic->f);
  243. av_buffer_unref(&pic->hwaccel_priv_buf);
  244. if (pic->needs_realloc)
  245. ff_free_picture_tables(pic);
  246. }
  247. int ff_update_picture_tables(Picture *dst, Picture *src)
  248. {
  249. int i;
  250. #define UPDATE_TABLE(table) \
  251. do { \
  252. if (src->table && \
  253. (!dst->table || dst->table->buffer != src->table->buffer)) { \
  254. av_buffer_unref(&dst->table); \
  255. dst->table = av_buffer_ref(src->table); \
  256. if (!dst->table) { \
  257. ff_free_picture_tables(dst); \
  258. return AVERROR(ENOMEM); \
  259. } \
  260. } \
  261. } while (0)
  262. UPDATE_TABLE(mb_var_buf);
  263. UPDATE_TABLE(mc_mb_var_buf);
  264. UPDATE_TABLE(mb_mean_buf);
  265. UPDATE_TABLE(mbskip_table_buf);
  266. UPDATE_TABLE(qscale_table_buf);
  267. UPDATE_TABLE(mb_type_buf);
  268. for (i = 0; i < 2; i++) {
  269. UPDATE_TABLE(motion_val_buf[i]);
  270. UPDATE_TABLE(ref_index_buf[i]);
  271. }
  272. dst->mb_var = src->mb_var;
  273. dst->mc_mb_var = src->mc_mb_var;
  274. dst->mb_mean = src->mb_mean;
  275. dst->mbskip_table = src->mbskip_table;
  276. dst->qscale_table = src->qscale_table;
  277. dst->mb_type = src->mb_type;
  278. for (i = 0; i < 2; i++) {
  279. dst->motion_val[i] = src->motion_val[i];
  280. dst->ref_index[i] = src->ref_index[i];
  281. }
  282. return 0;
  283. }
  284. int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
  285. {
  286. int ret;
  287. av_assert0(!dst->f->buf[0]);
  288. av_assert0(src->f->buf[0]);
  289. src->tf.f = src->f;
  290. dst->tf.f = dst->f;
  291. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  292. if (ret < 0)
  293. goto fail;
  294. ret = ff_update_picture_tables(dst, src);
  295. if (ret < 0)
  296. goto fail;
  297. if (src->hwaccel_picture_private) {
  298. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  299. if (!dst->hwaccel_priv_buf)
  300. goto fail;
  301. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  302. }
  303. dst->field_picture = src->field_picture;
  304. dst->mb_var_sum = src->mb_var_sum;
  305. dst->mc_mb_var_sum = src->mc_mb_var_sum;
  306. dst->b_frame_score = src->b_frame_score;
  307. dst->needs_realloc = src->needs_realloc;
  308. dst->reference = src->reference;
  309. dst->shared = src->shared;
  310. memcpy(dst->encoding_error, src->encoding_error,
  311. sizeof(dst->encoding_error));
  312. return 0;
  313. fail:
  314. ff_mpeg_unref_picture(avctx, dst);
  315. return ret;
  316. }
  317. static inline int pic_is_unused(Picture *pic)
  318. {
  319. if (!pic->f->buf[0])
  320. return 1;
  321. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  322. return 1;
  323. return 0;
  324. }
  325. static int find_unused_picture(Picture *picture, int shared)
  326. {
  327. int i;
  328. if (shared) {
  329. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  330. if (!picture[i].f->buf[0])
  331. return i;
  332. }
  333. } else {
  334. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  335. if (pic_is_unused(&picture[i]))
  336. return i;
  337. }
  338. }
  339. return AVERROR_INVALIDDATA;
  340. }
  341. int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
  342. {
  343. int ret = find_unused_picture(picture, shared);
  344. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  345. if (picture[ret].needs_realloc) {
  346. picture[ret].needs_realloc = 0;
  347. ff_free_picture_tables(&picture[ret]);
  348. ff_mpeg_unref_picture(avctx, &picture[ret]);
  349. }
  350. }
  351. return ret;
  352. }
  353. void ff_free_picture_tables(Picture *pic)
  354. {
  355. int i;
  356. av_buffer_unref(&pic->mb_var_buf);
  357. av_buffer_unref(&pic->mc_mb_var_buf);
  358. av_buffer_unref(&pic->mb_mean_buf);
  359. av_buffer_unref(&pic->mbskip_table_buf);
  360. av_buffer_unref(&pic->qscale_table_buf);
  361. av_buffer_unref(&pic->mb_type_buf);
  362. for (i = 0; i < 2; i++) {
  363. av_buffer_unref(&pic->motion_val_buf[i]);
  364. av_buffer_unref(&pic->ref_index_buf[i]);
  365. }
  366. }