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.

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