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.

957 lines
28KB

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