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.

970 lines
29KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "channel_layout.h"
  19. #include "avassert.h"
  20. #include "buffer.h"
  21. #include "common.h"
  22. #include "dict.h"
  23. #include "frame.h"
  24. #include "imgutils.h"
  25. #include "mem.h"
  26. #include "samplefmt.h"
  27. #include "hwcontext.h"
  28. #if FF_API_FRAME_GET_SET
  29. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  30. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  31. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  32. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  33. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  34. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  35. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  36. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  37. MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
  38. MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
  39. MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
  40. #endif
  41. #define CHECK_CHANNELS_CONSISTENCY(frame) \
  42. av_assert2(!(frame)->channel_layout || \
  43. (frame)->channels == \
  44. av_get_channel_layout_nb_channels((frame)->channel_layout))
  45. #if FF_API_FRAME_QP
  46. struct qp_properties {
  47. int stride;
  48. int type;
  49. };
  50. int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
  51. {
  52. struct qp_properties *p;
  53. AVFrameSideData *sd;
  54. AVBufferRef *ref;
  55. FF_DISABLE_DEPRECATION_WARNINGS
  56. av_buffer_unref(&f->qp_table_buf);
  57. f->qp_table_buf = buf;
  58. f->qscale_table = buf->data;
  59. f->qstride = stride;
  60. f->qscale_type = qp_type;
  61. FF_ENABLE_DEPRECATION_WARNINGS
  62. av_frame_remove_side_data(f, AV_FRAME_DATA_QP_TABLE_PROPERTIES);
  63. av_frame_remove_side_data(f, AV_FRAME_DATA_QP_TABLE_DATA);
  64. ref = av_buffer_ref(buf);
  65. if (!av_frame_new_side_data_from_buf(f, AV_FRAME_DATA_QP_TABLE_DATA, ref)) {
  66. av_buffer_unref(&ref);
  67. return AVERROR(ENOMEM);
  68. }
  69. sd = av_frame_new_side_data(f, AV_FRAME_DATA_QP_TABLE_PROPERTIES,
  70. sizeof(struct qp_properties));
  71. if (!sd)
  72. return AVERROR(ENOMEM);
  73. p = (struct qp_properties *)sd->data;
  74. p->stride = stride;
  75. p->type = qp_type;
  76. return 0;
  77. }
  78. int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
  79. {
  80. AVBufferRef *buf = NULL;
  81. *stride = 0;
  82. *type = 0;
  83. FF_DISABLE_DEPRECATION_WARNINGS
  84. if (f->qp_table_buf) {
  85. *stride = f->qstride;
  86. *type = f->qscale_type;
  87. buf = f->qp_table_buf;
  88. FF_ENABLE_DEPRECATION_WARNINGS
  89. } else {
  90. AVFrameSideData *sd;
  91. struct qp_properties *p;
  92. sd = av_frame_get_side_data(f, AV_FRAME_DATA_QP_TABLE_PROPERTIES);
  93. if (!sd)
  94. return NULL;
  95. p = (struct qp_properties *)sd->data;
  96. sd = av_frame_get_side_data(f, AV_FRAME_DATA_QP_TABLE_DATA);
  97. if (!sd)
  98. return NULL;
  99. *stride = p->stride;
  100. *type = p->type;
  101. buf = sd->buf;
  102. }
  103. return buf ? buf->data : NULL;
  104. }
  105. #endif
  106. const char *av_get_colorspace_name(enum AVColorSpace val)
  107. {
  108. static const char * const name[] = {
  109. [AVCOL_SPC_RGB] = "GBR",
  110. [AVCOL_SPC_BT709] = "bt709",
  111. [AVCOL_SPC_FCC] = "fcc",
  112. [AVCOL_SPC_BT470BG] = "bt470bg",
  113. [AVCOL_SPC_SMPTE170M] = "smpte170m",
  114. [AVCOL_SPC_SMPTE240M] = "smpte240m",
  115. [AVCOL_SPC_YCOCG] = "YCgCo",
  116. };
  117. if ((unsigned)val >= FF_ARRAY_ELEMS(name))
  118. return NULL;
  119. return name[val];
  120. }
  121. static void get_frame_defaults(AVFrame *frame)
  122. {
  123. if (frame->extended_data != frame->data)
  124. av_freep(&frame->extended_data);
  125. memset(frame, 0, sizeof(*frame));
  126. frame->pts =
  127. frame->pkt_dts = AV_NOPTS_VALUE;
  128. #if FF_API_PKT_PTS
  129. FF_DISABLE_DEPRECATION_WARNINGS
  130. frame->pkt_pts = AV_NOPTS_VALUE;
  131. FF_ENABLE_DEPRECATION_WARNINGS
  132. #endif
  133. frame->best_effort_timestamp = AV_NOPTS_VALUE;
  134. frame->pkt_duration = 0;
  135. frame->pkt_pos = -1;
  136. frame->pkt_size = -1;
  137. frame->key_frame = 1;
  138. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  139. frame->format = -1; /* unknown */
  140. frame->extended_data = frame->data;
  141. frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
  142. frame->color_trc = AVCOL_TRC_UNSPECIFIED;
  143. frame->colorspace = AVCOL_SPC_UNSPECIFIED;
  144. frame->color_range = AVCOL_RANGE_UNSPECIFIED;
  145. frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  146. frame->flags = 0;
  147. }
  148. static void free_side_data(AVFrameSideData **ptr_sd)
  149. {
  150. AVFrameSideData *sd = *ptr_sd;
  151. av_buffer_unref(&sd->buf);
  152. av_dict_free(&sd->metadata);
  153. av_freep(ptr_sd);
  154. }
  155. static void wipe_side_data(AVFrame *frame)
  156. {
  157. int i;
  158. for (i = 0; i < frame->nb_side_data; i++) {
  159. free_side_data(&frame->side_data[i]);
  160. }
  161. frame->nb_side_data = 0;
  162. av_freep(&frame->side_data);
  163. }
  164. AVFrame *av_frame_alloc(void)
  165. {
  166. AVFrame *frame = av_mallocz(sizeof(*frame));
  167. if (!frame)
  168. return NULL;
  169. frame->extended_data = NULL;
  170. get_frame_defaults(frame);
  171. return frame;
  172. }
  173. void av_frame_free(AVFrame **frame)
  174. {
  175. if (!frame || !*frame)
  176. return;
  177. av_frame_unref(*frame);
  178. av_freep(frame);
  179. }
  180. static int get_video_buffer(AVFrame *frame, int align)
  181. {
  182. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  183. int ret, i, padded_height, total_size;
  184. int plane_padding = FFMAX(16 + 16/*STRIDE_ALIGN*/, align);
  185. ptrdiff_t linesizes[4];
  186. size_t sizes[4];
  187. if (!desc)
  188. return AVERROR(EINVAL);
  189. if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
  190. return ret;
  191. if (!frame->linesize[0]) {
  192. if (align <= 0)
  193. align = 32; /* STRIDE_ALIGN. Should be av_cpu_max_align() */
  194. for(i=1; i<=align; i+=i) {
  195. ret = av_image_fill_linesizes(frame->linesize, frame->format,
  196. FFALIGN(frame->width, i));
  197. if (ret < 0)
  198. return ret;
  199. if (!(frame->linesize[0] & (align-1)))
  200. break;
  201. }
  202. for (i = 0; i < 4 && frame->linesize[i]; i++)
  203. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  204. }
  205. for (i = 0; i < 4; i++)
  206. linesizes[i] = frame->linesize[i];
  207. padded_height = FFALIGN(frame->height, 32);
  208. if ((ret = av_image_fill_plane_sizes(sizes, frame->format,
  209. padded_height, linesizes)) < 0)
  210. return ret;
  211. total_size = 4*plane_padding;
  212. for (i = 0; i < 4; i++) {
  213. if (sizes[i] > INT_MAX - total_size)
  214. return AVERROR(EINVAL);
  215. total_size += sizes[i];
  216. }
  217. frame->buf[0] = av_buffer_alloc(total_size);
  218. if (!frame->buf[0]) {
  219. ret = AVERROR(ENOMEM);
  220. goto fail;
  221. }
  222. if ((ret = av_image_fill_pointers(frame->data, frame->format, padded_height,
  223. frame->buf[0]->data, frame->linesize)) < 0)
  224. goto fail;
  225. for (i = 1; i < 4; i++) {
  226. if (frame->data[i])
  227. frame->data[i] += i * plane_padding;
  228. }
  229. frame->extended_data = frame->data;
  230. return 0;
  231. fail:
  232. av_frame_unref(frame);
  233. return ret;
  234. }
  235. static int get_audio_buffer(AVFrame *frame, int align)
  236. {
  237. int channels;
  238. int planar = av_sample_fmt_is_planar(frame->format);
  239. int planes;
  240. int ret, i;
  241. if (!frame->channels)
  242. frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  243. channels = frame->channels;
  244. planes = planar ? channels : 1;
  245. CHECK_CHANNELS_CONSISTENCY(frame);
  246. if (!frame->linesize[0]) {
  247. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  248. frame->nb_samples, frame->format,
  249. align);
  250. if (ret < 0)
  251. return ret;
  252. }
  253. if (planes > AV_NUM_DATA_POINTERS) {
  254. frame->extended_data = av_mallocz_array(planes,
  255. sizeof(*frame->extended_data));
  256. frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
  257. sizeof(*frame->extended_buf));
  258. if (!frame->extended_data || !frame->extended_buf) {
  259. av_freep(&frame->extended_data);
  260. av_freep(&frame->extended_buf);
  261. return AVERROR(ENOMEM);
  262. }
  263. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  264. } else
  265. frame->extended_data = frame->data;
  266. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  267. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  268. if (!frame->buf[i]) {
  269. av_frame_unref(frame);
  270. return AVERROR(ENOMEM);
  271. }
  272. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  273. }
  274. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  275. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  276. if (!frame->extended_buf[i]) {
  277. av_frame_unref(frame);
  278. return AVERROR(ENOMEM);
  279. }
  280. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  281. }
  282. return 0;
  283. }
  284. int av_frame_get_buffer(AVFrame *frame, int align)
  285. {
  286. if (frame->format < 0)
  287. return AVERROR(EINVAL);
  288. if (frame->width > 0 && frame->height > 0)
  289. return get_video_buffer(frame, align);
  290. else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
  291. return get_audio_buffer(frame, align);
  292. return AVERROR(EINVAL);
  293. }
  294. static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
  295. {
  296. int i;
  297. dst->key_frame = src->key_frame;
  298. dst->pict_type = src->pict_type;
  299. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  300. dst->crop_top = src->crop_top;
  301. dst->crop_bottom = src->crop_bottom;
  302. dst->crop_left = src->crop_left;
  303. dst->crop_right = src->crop_right;
  304. dst->pts = src->pts;
  305. dst->repeat_pict = src->repeat_pict;
  306. dst->interlaced_frame = src->interlaced_frame;
  307. dst->top_field_first = src->top_field_first;
  308. dst->palette_has_changed = src->palette_has_changed;
  309. dst->sample_rate = src->sample_rate;
  310. dst->opaque = src->opaque;
  311. #if FF_API_PKT_PTS
  312. FF_DISABLE_DEPRECATION_WARNINGS
  313. dst->pkt_pts = src->pkt_pts;
  314. FF_ENABLE_DEPRECATION_WARNINGS
  315. #endif
  316. dst->pkt_dts = src->pkt_dts;
  317. dst->pkt_pos = src->pkt_pos;
  318. dst->pkt_size = src->pkt_size;
  319. dst->pkt_duration = src->pkt_duration;
  320. dst->reordered_opaque = src->reordered_opaque;
  321. dst->quality = src->quality;
  322. dst->best_effort_timestamp = src->best_effort_timestamp;
  323. dst->coded_picture_number = src->coded_picture_number;
  324. dst->display_picture_number = src->display_picture_number;
  325. dst->flags = src->flags;
  326. dst->decode_error_flags = src->decode_error_flags;
  327. dst->color_primaries = src->color_primaries;
  328. dst->color_trc = src->color_trc;
  329. dst->colorspace = src->colorspace;
  330. dst->color_range = src->color_range;
  331. dst->chroma_location = src->chroma_location;
  332. av_dict_copy(&dst->metadata, src->metadata, 0);
  333. #if FF_API_ERROR_FRAME
  334. FF_DISABLE_DEPRECATION_WARNINGS
  335. memcpy(dst->error, src->error, sizeof(dst->error));
  336. FF_ENABLE_DEPRECATION_WARNINGS
  337. #endif
  338. for (i = 0; i < src->nb_side_data; i++) {
  339. const AVFrameSideData *sd_src = src->side_data[i];
  340. AVFrameSideData *sd_dst;
  341. if ( sd_src->type == AV_FRAME_DATA_PANSCAN
  342. && (src->width != dst->width || src->height != dst->height))
  343. continue;
  344. if (force_copy) {
  345. sd_dst = av_frame_new_side_data(dst, sd_src->type,
  346. sd_src->size);
  347. if (!sd_dst) {
  348. wipe_side_data(dst);
  349. return AVERROR(ENOMEM);
  350. }
  351. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  352. } else {
  353. AVBufferRef *ref = av_buffer_ref(sd_src->buf);
  354. sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref);
  355. if (!sd_dst) {
  356. av_buffer_unref(&ref);
  357. wipe_side_data(dst);
  358. return AVERROR(ENOMEM);
  359. }
  360. }
  361. av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
  362. }
  363. #if FF_API_FRAME_QP
  364. FF_DISABLE_DEPRECATION_WARNINGS
  365. dst->qscale_table = NULL;
  366. dst->qstride = 0;
  367. dst->qscale_type = 0;
  368. av_buffer_unref(&dst->qp_table_buf);
  369. if (src->qp_table_buf) {
  370. dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
  371. if (dst->qp_table_buf) {
  372. dst->qscale_table = dst->qp_table_buf->data;
  373. dst->qstride = src->qstride;
  374. dst->qscale_type = src->qscale_type;
  375. }
  376. }
  377. FF_ENABLE_DEPRECATION_WARNINGS
  378. #endif
  379. av_buffer_unref(&dst->opaque_ref);
  380. av_buffer_unref(&dst->private_ref);
  381. if (src->opaque_ref) {
  382. dst->opaque_ref = av_buffer_ref(src->opaque_ref);
  383. if (!dst->opaque_ref)
  384. return AVERROR(ENOMEM);
  385. }
  386. if (src->private_ref) {
  387. dst->private_ref = av_buffer_ref(src->private_ref);
  388. if (!dst->private_ref)
  389. return AVERROR(ENOMEM);
  390. }
  391. return 0;
  392. }
  393. int av_frame_ref(AVFrame *dst, const AVFrame *src)
  394. {
  395. int i, ret = 0;
  396. av_assert1(dst->width == 0 && dst->height == 0);
  397. av_assert1(dst->channels == 0);
  398. dst->format = src->format;
  399. dst->width = src->width;
  400. dst->height = src->height;
  401. dst->channels = src->channels;
  402. dst->channel_layout = src->channel_layout;
  403. dst->nb_samples = src->nb_samples;
  404. ret = frame_copy_props(dst, src, 0);
  405. if (ret < 0)
  406. return ret;
  407. /* duplicate the frame data if it's not refcounted */
  408. if (!src->buf[0]) {
  409. ret = av_frame_get_buffer(dst, 0);
  410. if (ret < 0)
  411. return ret;
  412. ret = av_frame_copy(dst, src);
  413. if (ret < 0)
  414. av_frame_unref(dst);
  415. return ret;
  416. }
  417. /* ref the buffers */
  418. for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
  419. if (!src->buf[i])
  420. continue;
  421. dst->buf[i] = av_buffer_ref(src->buf[i]);
  422. if (!dst->buf[i]) {
  423. ret = AVERROR(ENOMEM);
  424. goto fail;
  425. }
  426. }
  427. if (src->extended_buf) {
  428. dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
  429. src->nb_extended_buf);
  430. if (!dst->extended_buf) {
  431. ret = AVERROR(ENOMEM);
  432. goto fail;
  433. }
  434. dst->nb_extended_buf = src->nb_extended_buf;
  435. for (i = 0; i < src->nb_extended_buf; i++) {
  436. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  437. if (!dst->extended_buf[i]) {
  438. ret = AVERROR(ENOMEM);
  439. goto fail;
  440. }
  441. }
  442. }
  443. if (src->hw_frames_ctx) {
  444. dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
  445. if (!dst->hw_frames_ctx) {
  446. ret = AVERROR(ENOMEM);
  447. goto fail;
  448. }
  449. }
  450. /* duplicate extended data */
  451. if (src->extended_data != src->data) {
  452. int ch = src->channels;
  453. if (!ch) {
  454. ret = AVERROR(EINVAL);
  455. goto fail;
  456. }
  457. CHECK_CHANNELS_CONSISTENCY(src);
  458. dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
  459. if (!dst->extended_data) {
  460. ret = AVERROR(ENOMEM);
  461. goto fail;
  462. }
  463. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  464. } else
  465. dst->extended_data = dst->data;
  466. memcpy(dst->data, src->data, sizeof(src->data));
  467. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  468. return 0;
  469. fail:
  470. av_frame_unref(dst);
  471. return ret;
  472. }
  473. AVFrame *av_frame_clone(const AVFrame *src)
  474. {
  475. AVFrame *ret = av_frame_alloc();
  476. if (!ret)
  477. return NULL;
  478. if (av_frame_ref(ret, src) < 0)
  479. av_frame_free(&ret);
  480. return ret;
  481. }
  482. void av_frame_unref(AVFrame *frame)
  483. {
  484. int i;
  485. if (!frame)
  486. return;
  487. wipe_side_data(frame);
  488. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  489. av_buffer_unref(&frame->buf[i]);
  490. for (i = 0; i < frame->nb_extended_buf; i++)
  491. av_buffer_unref(&frame->extended_buf[i]);
  492. av_freep(&frame->extended_buf);
  493. av_dict_free(&frame->metadata);
  494. #if FF_API_FRAME_QP
  495. FF_DISABLE_DEPRECATION_WARNINGS
  496. av_buffer_unref(&frame->qp_table_buf);
  497. FF_ENABLE_DEPRECATION_WARNINGS
  498. #endif
  499. av_buffer_unref(&frame->hw_frames_ctx);
  500. av_buffer_unref(&frame->opaque_ref);
  501. av_buffer_unref(&frame->private_ref);
  502. get_frame_defaults(frame);
  503. }
  504. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  505. {
  506. av_assert1(dst->width == 0 && dst->height == 0);
  507. av_assert1(dst->channels == 0);
  508. *dst = *src;
  509. if (src->extended_data == src->data)
  510. dst->extended_data = dst->data;
  511. memset(src, 0, sizeof(*src));
  512. get_frame_defaults(src);
  513. }
  514. int av_frame_is_writable(AVFrame *frame)
  515. {
  516. int i, ret = 1;
  517. /* assume non-refcounted frames are not writable */
  518. if (!frame->buf[0])
  519. return 0;
  520. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  521. if (frame->buf[i])
  522. ret &= !!av_buffer_is_writable(frame->buf[i]);
  523. for (i = 0; i < frame->nb_extended_buf; i++)
  524. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  525. return ret;
  526. }
  527. int av_frame_make_writable(AVFrame *frame)
  528. {
  529. AVFrame tmp;
  530. int ret;
  531. if (!frame->buf[0])
  532. return AVERROR(EINVAL);
  533. if (av_frame_is_writable(frame))
  534. return 0;
  535. memset(&tmp, 0, sizeof(tmp));
  536. tmp.format = frame->format;
  537. tmp.width = frame->width;
  538. tmp.height = frame->height;
  539. tmp.channels = frame->channels;
  540. tmp.channel_layout = frame->channel_layout;
  541. tmp.nb_samples = frame->nb_samples;
  542. if (frame->hw_frames_ctx)
  543. ret = av_hwframe_get_buffer(frame->hw_frames_ctx, &tmp, 0);
  544. else
  545. ret = av_frame_get_buffer(&tmp, 0);
  546. if (ret < 0)
  547. return ret;
  548. ret = av_frame_copy(&tmp, frame);
  549. if (ret < 0) {
  550. av_frame_unref(&tmp);
  551. return ret;
  552. }
  553. ret = av_frame_copy_props(&tmp, frame);
  554. if (ret < 0) {
  555. av_frame_unref(&tmp);
  556. return ret;
  557. }
  558. av_frame_unref(frame);
  559. *frame = tmp;
  560. if (tmp.data == tmp.extended_data)
  561. frame->extended_data = frame->data;
  562. return 0;
  563. }
  564. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  565. {
  566. return frame_copy_props(dst, src, 1);
  567. }
  568. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  569. {
  570. uint8_t *data;
  571. int planes, i;
  572. if (frame->nb_samples) {
  573. int channels = frame->channels;
  574. if (!channels)
  575. return NULL;
  576. CHECK_CHANNELS_CONSISTENCY(frame);
  577. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  578. } else
  579. planes = 4;
  580. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  581. return NULL;
  582. data = frame->extended_data[plane];
  583. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  584. AVBufferRef *buf = frame->buf[i];
  585. if (data >= buf->data && data < buf->data + buf->size)
  586. return buf;
  587. }
  588. for (i = 0; i < frame->nb_extended_buf; i++) {
  589. AVBufferRef *buf = frame->extended_buf[i];
  590. if (data >= buf->data && data < buf->data + buf->size)
  591. return buf;
  592. }
  593. return NULL;
  594. }
  595. AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame,
  596. enum AVFrameSideDataType type,
  597. AVBufferRef *buf)
  598. {
  599. AVFrameSideData *ret, **tmp;
  600. if (!buf)
  601. return NULL;
  602. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  603. return NULL;
  604. tmp = av_realloc(frame->side_data,
  605. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  606. if (!tmp)
  607. return NULL;
  608. frame->side_data = tmp;
  609. ret = av_mallocz(sizeof(*ret));
  610. if (!ret)
  611. return NULL;
  612. ret->buf = buf;
  613. ret->data = ret->buf->data;
  614. ret->size = buf->size;
  615. ret->type = type;
  616. frame->side_data[frame->nb_side_data++] = ret;
  617. return ret;
  618. }
  619. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  620. enum AVFrameSideDataType type,
  621. int size)
  622. {
  623. AVFrameSideData *ret;
  624. AVBufferRef *buf = av_buffer_alloc(size);
  625. ret = av_frame_new_side_data_from_buf(frame, type, buf);
  626. if (!ret)
  627. av_buffer_unref(&buf);
  628. return ret;
  629. }
  630. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  631. enum AVFrameSideDataType type)
  632. {
  633. int i;
  634. for (i = 0; i < frame->nb_side_data; i++) {
  635. if (frame->side_data[i]->type == type)
  636. return frame->side_data[i];
  637. }
  638. return NULL;
  639. }
  640. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  641. {
  642. const uint8_t *src_data[4];
  643. int i, planes;
  644. if (dst->width < src->width ||
  645. dst->height < src->height)
  646. return AVERROR(EINVAL);
  647. if (src->hw_frames_ctx || dst->hw_frames_ctx)
  648. return av_hwframe_transfer_data(dst, src, 0);
  649. planes = av_pix_fmt_count_planes(dst->format);
  650. for (i = 0; i < planes; i++)
  651. if (!dst->data[i] || !src->data[i])
  652. return AVERROR(EINVAL);
  653. memcpy(src_data, src->data, sizeof(src_data));
  654. av_image_copy(dst->data, dst->linesize,
  655. src_data, src->linesize,
  656. dst->format, src->width, src->height);
  657. return 0;
  658. }
  659. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  660. {
  661. int planar = av_sample_fmt_is_planar(dst->format);
  662. int channels = dst->channels;
  663. int planes = planar ? channels : 1;
  664. int i;
  665. if (dst->nb_samples != src->nb_samples ||
  666. dst->channels != src->channels ||
  667. dst->channel_layout != src->channel_layout)
  668. return AVERROR(EINVAL);
  669. CHECK_CHANNELS_CONSISTENCY(src);
  670. for (i = 0; i < planes; i++)
  671. if (!dst->extended_data[i] || !src->extended_data[i])
  672. return AVERROR(EINVAL);
  673. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  674. dst->nb_samples, channels, dst->format);
  675. return 0;
  676. }
  677. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  678. {
  679. if (dst->format != src->format || dst->format < 0)
  680. return AVERROR(EINVAL);
  681. if (dst->width > 0 && dst->height > 0)
  682. return frame_copy_video(dst, src);
  683. else if (dst->nb_samples > 0 && dst->channels > 0)
  684. return frame_copy_audio(dst, src);
  685. return AVERROR(EINVAL);
  686. }
  687. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  688. {
  689. int i;
  690. for (i = frame->nb_side_data - 1; i >= 0; i--) {
  691. AVFrameSideData *sd = frame->side_data[i];
  692. if (sd->type == type) {
  693. free_side_data(&frame->side_data[i]);
  694. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  695. frame->nb_side_data--;
  696. }
  697. }
  698. }
  699. const char *av_frame_side_data_name(enum AVFrameSideDataType type)
  700. {
  701. switch(type) {
  702. case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
  703. case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
  704. case AV_FRAME_DATA_STEREO3D: return "Stereo 3D";
  705. case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
  706. case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
  707. case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
  708. case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
  709. case AV_FRAME_DATA_AFD: return "Active format description";
  710. case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
  711. case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
  712. case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
  713. case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
  714. case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
  715. case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";
  716. case AV_FRAME_DATA_S12M_TIMECODE: return "SMPTE 12-1 timecode";
  717. case AV_FRAME_DATA_SPHERICAL: return "Spherical Mapping";
  718. case AV_FRAME_DATA_ICC_PROFILE: return "ICC profile";
  719. #if FF_API_FRAME_QP
  720. case AV_FRAME_DATA_QP_TABLE_PROPERTIES: return "QP table properties";
  721. case AV_FRAME_DATA_QP_TABLE_DATA: return "QP table data";
  722. #endif
  723. case AV_FRAME_DATA_DYNAMIC_HDR_PLUS: return "HDR Dynamic Metadata SMPTE2094-40 (HDR10+)";
  724. case AV_FRAME_DATA_REGIONS_OF_INTEREST: return "Regions Of Interest";
  725. case AV_FRAME_DATA_VIDEO_ENC_PARAMS: return "Video encoding parameters";
  726. case AV_FRAME_DATA_SEI_UNREGISTERED: return "H.26[45] User Data Unregistered SEI message";
  727. }
  728. return NULL;
  729. }
  730. static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
  731. const AVPixFmtDescriptor *desc)
  732. {
  733. int i, j;
  734. for (i = 0; frame->data[i]; i++) {
  735. const AVComponentDescriptor *comp = NULL;
  736. int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
  737. int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  738. if (desc->flags & (AV_PIX_FMT_FLAG_PAL | FF_PSEUDOPAL) && i == 1) {
  739. offsets[i] = 0;
  740. break;
  741. }
  742. /* find any component descriptor for this plane */
  743. for (j = 0; j < desc->nb_components; j++) {
  744. if (desc->comp[j].plane == i) {
  745. comp = &desc->comp[j];
  746. break;
  747. }
  748. }
  749. if (!comp)
  750. return AVERROR_BUG;
  751. offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] +
  752. (frame->crop_left >> shift_x) * comp->step;
  753. }
  754. return 0;
  755. }
  756. int av_frame_apply_cropping(AVFrame *frame, int flags)
  757. {
  758. const AVPixFmtDescriptor *desc;
  759. size_t offsets[4];
  760. int i;
  761. if (!(frame->width > 0 && frame->height > 0))
  762. return AVERROR(EINVAL);
  763. if (frame->crop_left >= INT_MAX - frame->crop_right ||
  764. frame->crop_top >= INT_MAX - frame->crop_bottom ||
  765. (frame->crop_left + frame->crop_right) >= frame->width ||
  766. (frame->crop_top + frame->crop_bottom) >= frame->height)
  767. return AVERROR(ERANGE);
  768. desc = av_pix_fmt_desc_get(frame->format);
  769. if (!desc)
  770. return AVERROR_BUG;
  771. /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
  772. * formats cannot be easily handled here either (and corresponding decoders
  773. * should not export any cropping anyway), so do the same for those as well.
  774. * */
  775. if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
  776. frame->width -= frame->crop_right;
  777. frame->height -= frame->crop_bottom;
  778. frame->crop_right = 0;
  779. frame->crop_bottom = 0;
  780. return 0;
  781. }
  782. /* calculate the offsets for each plane */
  783. calc_cropping_offsets(offsets, frame, desc);
  784. /* adjust the offsets to avoid breaking alignment */
  785. if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
  786. int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
  787. int min_log2_align = INT_MAX;
  788. for (i = 0; frame->data[i]; i++) {
  789. int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
  790. min_log2_align = FFMIN(log2_align, min_log2_align);
  791. }
  792. /* we assume, and it should always be true, that the data alignment is
  793. * related to the cropping alignment by a constant power-of-2 factor */
  794. if (log2_crop_align < min_log2_align)
  795. return AVERROR_BUG;
  796. if (min_log2_align < 5) {
  797. frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
  798. calc_cropping_offsets(offsets, frame, desc);
  799. }
  800. }
  801. for (i = 0; frame->data[i]; i++)
  802. frame->data[i] += offsets[i];
  803. frame->width -= (frame->crop_left + frame->crop_right);
  804. frame->height -= (frame->crop_top + frame->crop_bottom);
  805. frame->crop_left = 0;
  806. frame->crop_right = 0;
  807. frame->crop_top = 0;
  808. frame->crop_bottom = 0;
  809. return 0;
  810. }