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.

958 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 ret, 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_replace(&dst->qp_table_buf, src->qp_table_buf);
  369. if (dst->qp_table_buf) {
  370. dst->qscale_table = dst->qp_table_buf->data;
  371. dst->qstride = src->qstride;
  372. dst->qscale_type = src->qscale_type;
  373. }
  374. FF_ENABLE_DEPRECATION_WARNINGS
  375. #endif
  376. ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
  377. ret |= av_buffer_replace(&dst->private_ref, src->private_ref);
  378. return ret;
  379. }
  380. int av_frame_ref(AVFrame *dst, const AVFrame *src)
  381. {
  382. int i, ret = 0;
  383. av_assert1(dst->width == 0 && dst->height == 0);
  384. av_assert1(dst->channels == 0);
  385. dst->format = src->format;
  386. dst->width = src->width;
  387. dst->height = src->height;
  388. dst->channels = src->channels;
  389. dst->channel_layout = src->channel_layout;
  390. dst->nb_samples = src->nb_samples;
  391. ret = frame_copy_props(dst, src, 0);
  392. if (ret < 0)
  393. goto fail;
  394. /* duplicate the frame data if it's not refcounted */
  395. if (!src->buf[0]) {
  396. ret = av_frame_get_buffer(dst, 0);
  397. if (ret < 0)
  398. goto fail;
  399. ret = av_frame_copy(dst, src);
  400. if (ret < 0)
  401. goto fail;
  402. return ret;
  403. }
  404. /* ref the buffers */
  405. for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
  406. if (!src->buf[i])
  407. continue;
  408. dst->buf[i] = av_buffer_ref(src->buf[i]);
  409. if (!dst->buf[i]) {
  410. ret = AVERROR(ENOMEM);
  411. goto fail;
  412. }
  413. }
  414. if (src->extended_buf) {
  415. dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
  416. src->nb_extended_buf);
  417. if (!dst->extended_buf) {
  418. ret = AVERROR(ENOMEM);
  419. goto fail;
  420. }
  421. dst->nb_extended_buf = src->nb_extended_buf;
  422. for (i = 0; i < src->nb_extended_buf; i++) {
  423. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  424. if (!dst->extended_buf[i]) {
  425. ret = AVERROR(ENOMEM);
  426. goto fail;
  427. }
  428. }
  429. }
  430. if (src->hw_frames_ctx) {
  431. dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
  432. if (!dst->hw_frames_ctx) {
  433. ret = AVERROR(ENOMEM);
  434. goto fail;
  435. }
  436. }
  437. /* duplicate extended data */
  438. if (src->extended_data != src->data) {
  439. int ch = src->channels;
  440. if (!ch) {
  441. ret = AVERROR(EINVAL);
  442. goto fail;
  443. }
  444. CHECK_CHANNELS_CONSISTENCY(src);
  445. dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
  446. if (!dst->extended_data) {
  447. ret = AVERROR(ENOMEM);
  448. goto fail;
  449. }
  450. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  451. } else
  452. dst->extended_data = dst->data;
  453. memcpy(dst->data, src->data, sizeof(src->data));
  454. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  455. return 0;
  456. fail:
  457. av_frame_unref(dst);
  458. return ret;
  459. }
  460. AVFrame *av_frame_clone(const AVFrame *src)
  461. {
  462. AVFrame *ret = av_frame_alloc();
  463. if (!ret)
  464. return NULL;
  465. if (av_frame_ref(ret, src) < 0)
  466. av_frame_free(&ret);
  467. return ret;
  468. }
  469. void av_frame_unref(AVFrame *frame)
  470. {
  471. int i;
  472. if (!frame)
  473. return;
  474. wipe_side_data(frame);
  475. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  476. av_buffer_unref(&frame->buf[i]);
  477. for (i = 0; i < frame->nb_extended_buf; i++)
  478. av_buffer_unref(&frame->extended_buf[i]);
  479. av_freep(&frame->extended_buf);
  480. av_dict_free(&frame->metadata);
  481. #if FF_API_FRAME_QP
  482. FF_DISABLE_DEPRECATION_WARNINGS
  483. av_buffer_unref(&frame->qp_table_buf);
  484. FF_ENABLE_DEPRECATION_WARNINGS
  485. #endif
  486. av_buffer_unref(&frame->hw_frames_ctx);
  487. av_buffer_unref(&frame->opaque_ref);
  488. av_buffer_unref(&frame->private_ref);
  489. get_frame_defaults(frame);
  490. }
  491. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  492. {
  493. av_assert1(dst->width == 0 && dst->height == 0);
  494. av_assert1(dst->channels == 0);
  495. *dst = *src;
  496. if (src->extended_data == src->data)
  497. dst->extended_data = dst->data;
  498. memset(src, 0, sizeof(*src));
  499. get_frame_defaults(src);
  500. }
  501. int av_frame_is_writable(AVFrame *frame)
  502. {
  503. int i, ret = 1;
  504. /* assume non-refcounted frames are not writable */
  505. if (!frame->buf[0])
  506. return 0;
  507. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  508. if (frame->buf[i])
  509. ret &= !!av_buffer_is_writable(frame->buf[i]);
  510. for (i = 0; i < frame->nb_extended_buf; i++)
  511. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  512. return ret;
  513. }
  514. int av_frame_make_writable(AVFrame *frame)
  515. {
  516. AVFrame tmp;
  517. int ret;
  518. if (!frame->buf[0])
  519. return AVERROR(EINVAL);
  520. if (av_frame_is_writable(frame))
  521. return 0;
  522. memset(&tmp, 0, sizeof(tmp));
  523. tmp.format = frame->format;
  524. tmp.width = frame->width;
  525. tmp.height = frame->height;
  526. tmp.channels = frame->channels;
  527. tmp.channel_layout = frame->channel_layout;
  528. tmp.nb_samples = frame->nb_samples;
  529. if (frame->hw_frames_ctx)
  530. ret = av_hwframe_get_buffer(frame->hw_frames_ctx, &tmp, 0);
  531. else
  532. ret = av_frame_get_buffer(&tmp, 0);
  533. if (ret < 0)
  534. return ret;
  535. ret = av_frame_copy(&tmp, frame);
  536. if (ret < 0) {
  537. av_frame_unref(&tmp);
  538. return ret;
  539. }
  540. ret = av_frame_copy_props(&tmp, frame);
  541. if (ret < 0) {
  542. av_frame_unref(&tmp);
  543. return ret;
  544. }
  545. av_frame_unref(frame);
  546. *frame = tmp;
  547. if (tmp.data == tmp.extended_data)
  548. frame->extended_data = frame->data;
  549. return 0;
  550. }
  551. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  552. {
  553. return frame_copy_props(dst, src, 1);
  554. }
  555. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  556. {
  557. uint8_t *data;
  558. int planes, i;
  559. if (frame->nb_samples) {
  560. int channels = frame->channels;
  561. if (!channels)
  562. return NULL;
  563. CHECK_CHANNELS_CONSISTENCY(frame);
  564. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  565. } else
  566. planes = 4;
  567. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  568. return NULL;
  569. data = frame->extended_data[plane];
  570. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  571. AVBufferRef *buf = frame->buf[i];
  572. if (data >= buf->data && data < buf->data + buf->size)
  573. return buf;
  574. }
  575. for (i = 0; i < frame->nb_extended_buf; i++) {
  576. AVBufferRef *buf = frame->extended_buf[i];
  577. if (data >= buf->data && data < buf->data + buf->size)
  578. return buf;
  579. }
  580. return NULL;
  581. }
  582. AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame,
  583. enum AVFrameSideDataType type,
  584. AVBufferRef *buf)
  585. {
  586. AVFrameSideData *ret, **tmp;
  587. if (!buf)
  588. return NULL;
  589. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  590. return NULL;
  591. tmp = av_realloc(frame->side_data,
  592. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  593. if (!tmp)
  594. return NULL;
  595. frame->side_data = tmp;
  596. ret = av_mallocz(sizeof(*ret));
  597. if (!ret)
  598. return NULL;
  599. ret->buf = buf;
  600. ret->data = ret->buf->data;
  601. ret->size = buf->size;
  602. ret->type = type;
  603. frame->side_data[frame->nb_side_data++] = ret;
  604. return ret;
  605. }
  606. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  607. enum AVFrameSideDataType type,
  608. int size)
  609. {
  610. AVFrameSideData *ret;
  611. AVBufferRef *buf = av_buffer_alloc(size);
  612. ret = av_frame_new_side_data_from_buf(frame, type, buf);
  613. if (!ret)
  614. av_buffer_unref(&buf);
  615. return ret;
  616. }
  617. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  618. enum AVFrameSideDataType type)
  619. {
  620. int i;
  621. for (i = 0; i < frame->nb_side_data; i++) {
  622. if (frame->side_data[i]->type == type)
  623. return frame->side_data[i];
  624. }
  625. return NULL;
  626. }
  627. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  628. {
  629. const uint8_t *src_data[4];
  630. int i, planes;
  631. if (dst->width < src->width ||
  632. dst->height < src->height)
  633. return AVERROR(EINVAL);
  634. if (src->hw_frames_ctx || dst->hw_frames_ctx)
  635. return av_hwframe_transfer_data(dst, src, 0);
  636. planes = av_pix_fmt_count_planes(dst->format);
  637. for (i = 0; i < planes; i++)
  638. if (!dst->data[i] || !src->data[i])
  639. return AVERROR(EINVAL);
  640. memcpy(src_data, src->data, sizeof(src_data));
  641. av_image_copy(dst->data, dst->linesize,
  642. src_data, src->linesize,
  643. dst->format, src->width, src->height);
  644. return 0;
  645. }
  646. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  647. {
  648. int planar = av_sample_fmt_is_planar(dst->format);
  649. int channels = dst->channels;
  650. int planes = planar ? channels : 1;
  651. int i;
  652. if (dst->nb_samples != src->nb_samples ||
  653. dst->channels != src->channels ||
  654. dst->channel_layout != src->channel_layout)
  655. return AVERROR(EINVAL);
  656. CHECK_CHANNELS_CONSISTENCY(src);
  657. for (i = 0; i < planes; i++)
  658. if (!dst->extended_data[i] || !src->extended_data[i])
  659. return AVERROR(EINVAL);
  660. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  661. dst->nb_samples, channels, dst->format);
  662. return 0;
  663. }
  664. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  665. {
  666. if (dst->format != src->format || dst->format < 0)
  667. return AVERROR(EINVAL);
  668. if (dst->width > 0 && dst->height > 0)
  669. return frame_copy_video(dst, src);
  670. else if (dst->nb_samples > 0 && dst->channels > 0)
  671. return frame_copy_audio(dst, src);
  672. return AVERROR(EINVAL);
  673. }
  674. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  675. {
  676. int i;
  677. for (i = frame->nb_side_data - 1; i >= 0; i--) {
  678. AVFrameSideData *sd = frame->side_data[i];
  679. if (sd->type == type) {
  680. free_side_data(&frame->side_data[i]);
  681. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  682. frame->nb_side_data--;
  683. }
  684. }
  685. }
  686. const char *av_frame_side_data_name(enum AVFrameSideDataType type)
  687. {
  688. switch(type) {
  689. case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
  690. case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
  691. case AV_FRAME_DATA_STEREO3D: return "Stereo 3D";
  692. case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
  693. case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
  694. case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
  695. case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
  696. case AV_FRAME_DATA_AFD: return "Active format description";
  697. case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
  698. case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
  699. case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
  700. case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
  701. case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
  702. case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";
  703. case AV_FRAME_DATA_S12M_TIMECODE: return "SMPTE 12-1 timecode";
  704. case AV_FRAME_DATA_SPHERICAL: return "Spherical Mapping";
  705. case AV_FRAME_DATA_ICC_PROFILE: return "ICC profile";
  706. #if FF_API_FRAME_QP
  707. case AV_FRAME_DATA_QP_TABLE_PROPERTIES: return "QP table properties";
  708. case AV_FRAME_DATA_QP_TABLE_DATA: return "QP table data";
  709. #endif
  710. case AV_FRAME_DATA_DYNAMIC_HDR_PLUS: return "HDR Dynamic Metadata SMPTE2094-40 (HDR10+)";
  711. case AV_FRAME_DATA_REGIONS_OF_INTEREST: return "Regions Of Interest";
  712. case AV_FRAME_DATA_VIDEO_ENC_PARAMS: return "Video encoding parameters";
  713. case AV_FRAME_DATA_SEI_UNREGISTERED: return "H.26[45] User Data Unregistered SEI message";
  714. case AV_FRAME_DATA_FILM_GRAIN_PARAMS: return "Film grain parameters";
  715. }
  716. return NULL;
  717. }
  718. static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
  719. const AVPixFmtDescriptor *desc)
  720. {
  721. int i, j;
  722. for (i = 0; frame->data[i]; i++) {
  723. const AVComponentDescriptor *comp = NULL;
  724. int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
  725. int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  726. if (desc->flags & (AV_PIX_FMT_FLAG_PAL | FF_PSEUDOPAL) && i == 1) {
  727. offsets[i] = 0;
  728. break;
  729. }
  730. /* find any component descriptor for this plane */
  731. for (j = 0; j < desc->nb_components; j++) {
  732. if (desc->comp[j].plane == i) {
  733. comp = &desc->comp[j];
  734. break;
  735. }
  736. }
  737. if (!comp)
  738. return AVERROR_BUG;
  739. offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] +
  740. (frame->crop_left >> shift_x) * comp->step;
  741. }
  742. return 0;
  743. }
  744. int av_frame_apply_cropping(AVFrame *frame, int flags)
  745. {
  746. const AVPixFmtDescriptor *desc;
  747. size_t offsets[4];
  748. int i;
  749. if (!(frame->width > 0 && frame->height > 0))
  750. return AVERROR(EINVAL);
  751. if (frame->crop_left >= INT_MAX - frame->crop_right ||
  752. frame->crop_top >= INT_MAX - frame->crop_bottom ||
  753. (frame->crop_left + frame->crop_right) >= frame->width ||
  754. (frame->crop_top + frame->crop_bottom) >= frame->height)
  755. return AVERROR(ERANGE);
  756. desc = av_pix_fmt_desc_get(frame->format);
  757. if (!desc)
  758. return AVERROR_BUG;
  759. /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
  760. * formats cannot be easily handled here either (and corresponding decoders
  761. * should not export any cropping anyway), so do the same for those as well.
  762. * */
  763. if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
  764. frame->width -= frame->crop_right;
  765. frame->height -= frame->crop_bottom;
  766. frame->crop_right = 0;
  767. frame->crop_bottom = 0;
  768. return 0;
  769. }
  770. /* calculate the offsets for each plane */
  771. calc_cropping_offsets(offsets, frame, desc);
  772. /* adjust the offsets to avoid breaking alignment */
  773. if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
  774. int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
  775. int min_log2_align = INT_MAX;
  776. for (i = 0; frame->data[i]; i++) {
  777. int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
  778. min_log2_align = FFMIN(log2_align, min_log2_align);
  779. }
  780. /* we assume, and it should always be true, that the data alignment is
  781. * related to the cropping alignment by a constant power-of-2 factor */
  782. if (log2_crop_align < min_log2_align)
  783. return AVERROR_BUG;
  784. if (min_log2_align < 5) {
  785. frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
  786. calc_cropping_offsets(offsets, frame, desc);
  787. }
  788. }
  789. for (i = 0; frame->data[i]; i++)
  790. frame->data[i] += offsets[i];
  791. frame->width -= (frame->crop_left + frame->crop_right);
  792. frame->height -= (frame->crop_top + frame->crop_bottom);
  793. frame->crop_left = 0;
  794. frame->crop_right = 0;
  795. frame->crop_top = 0;
  796. frame->crop_bottom = 0;
  797. return 0;
  798. }