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.

735 lines
21KB

  1. /*
  2. *
  3. * This file is part of FFmpeg.
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with FFmpeg; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "channel_layout.h"
  20. #include "avassert.h"
  21. #include "buffer.h"
  22. #include "common.h"
  23. #include "dict.h"
  24. #include "frame.h"
  25. #include "imgutils.h"
  26. #include "mem.h"
  27. #include "samplefmt.h"
  28. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  29. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  30. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  31. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  32. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  33. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  34. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  35. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  36. MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
  37. MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
  38. MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
  39. #define CHECK_CHANNELS_CONSISTENCY(frame) \
  40. av_assert2(!(frame)->channel_layout || \
  41. (frame)->channels == \
  42. av_get_channel_layout_nb_channels((frame)->channel_layout))
  43. AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
  44. #if FF_API_FRAME_QP
  45. int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
  46. {
  47. av_buffer_unref(&f->qp_table_buf);
  48. f->qp_table_buf = buf;
  49. FF_DISABLE_DEPRECATION_WARNINGS
  50. f->qscale_table = buf->data;
  51. f->qstride = stride;
  52. f->qscale_type = qp_type;
  53. FF_ENABLE_DEPRECATION_WARNINGS
  54. return 0;
  55. }
  56. int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
  57. {
  58. FF_DISABLE_DEPRECATION_WARNINGS
  59. *stride = f->qstride;
  60. *type = f->qscale_type;
  61. FF_ENABLE_DEPRECATION_WARNINGS
  62. if (!f->qp_table_buf)
  63. return NULL;
  64. return f->qp_table_buf->data;
  65. }
  66. #endif
  67. const char *av_get_colorspace_name(enum AVColorSpace val)
  68. {
  69. static const char * const name[] = {
  70. [AVCOL_SPC_RGB] = "GBR",
  71. [AVCOL_SPC_BT709] = "bt709",
  72. [AVCOL_SPC_FCC] = "fcc",
  73. [AVCOL_SPC_BT470BG] = "bt470bg",
  74. [AVCOL_SPC_SMPTE170M] = "smpte170m",
  75. [AVCOL_SPC_SMPTE240M] = "smpte240m",
  76. [AVCOL_SPC_YCOCG] = "YCgCo",
  77. };
  78. if ((unsigned)val >= FF_ARRAY_ELEMS(name))
  79. return NULL;
  80. return name[val];
  81. }
  82. static void get_frame_defaults(AVFrame *frame)
  83. {
  84. if (frame->extended_data != frame->data)
  85. av_freep(&frame->extended_data);
  86. memset(frame, 0, sizeof(*frame));
  87. frame->pts =
  88. frame->pkt_dts =
  89. frame->pkt_pts = AV_NOPTS_VALUE;
  90. av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
  91. av_frame_set_pkt_duration (frame, 0);
  92. av_frame_set_pkt_pos (frame, -1);
  93. av_frame_set_pkt_size (frame, -1);
  94. frame->key_frame = 1;
  95. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  96. frame->format = -1; /* unknown */
  97. frame->extended_data = frame->data;
  98. frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
  99. frame->color_trc = AVCOL_TRC_UNSPECIFIED;
  100. frame->colorspace = AVCOL_SPC_UNSPECIFIED;
  101. frame->color_range = AVCOL_RANGE_UNSPECIFIED;
  102. frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  103. }
  104. static void free_side_data(AVFrameSideData **ptr_sd)
  105. {
  106. AVFrameSideData *sd = *ptr_sd;
  107. av_buffer_unref(&sd->buf);
  108. av_dict_free(&sd->metadata);
  109. av_freep(ptr_sd);
  110. }
  111. static void wipe_side_data(AVFrame *frame)
  112. {
  113. int i;
  114. for (i = 0; i < frame->nb_side_data; i++) {
  115. free_side_data(&frame->side_data[i]);
  116. }
  117. frame->nb_side_data = 0;
  118. av_freep(&frame->side_data);
  119. }
  120. AVFrame *av_frame_alloc(void)
  121. {
  122. AVFrame *frame = av_mallocz(sizeof(*frame));
  123. if (!frame)
  124. return NULL;
  125. frame->extended_data = NULL;
  126. get_frame_defaults(frame);
  127. return frame;
  128. }
  129. void av_frame_free(AVFrame **frame)
  130. {
  131. if (!frame || !*frame)
  132. return;
  133. av_frame_unref(*frame);
  134. av_freep(frame);
  135. }
  136. static int get_video_buffer(AVFrame *frame, int align)
  137. {
  138. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  139. int ret, i;
  140. if (!desc)
  141. return AVERROR(EINVAL);
  142. if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
  143. return ret;
  144. if (!frame->linesize[0]) {
  145. for(i=1; i<=align; i+=i) {
  146. ret = av_image_fill_linesizes(frame->linesize, frame->format,
  147. FFALIGN(frame->width, i));
  148. if (ret < 0)
  149. return ret;
  150. if (!(frame->linesize[0] & (align-1)))
  151. break;
  152. }
  153. for (i = 0; i < 4 && frame->linesize[i]; i++)
  154. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  155. }
  156. for (i = 0; i < 4 && frame->linesize[i]; i++) {
  157. int h = FFALIGN(frame->height, 32);
  158. if (i == 1 || i == 2)
  159. h = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
  160. frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
  161. if (!frame->buf[i])
  162. goto fail;
  163. frame->data[i] = frame->buf[i]->data;
  164. }
  165. if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  166. av_buffer_unref(&frame->buf[1]);
  167. frame->buf[1] = av_buffer_alloc(1024);
  168. if (!frame->buf[1])
  169. goto fail;
  170. frame->data[1] = frame->buf[1]->data;
  171. }
  172. frame->extended_data = frame->data;
  173. return 0;
  174. fail:
  175. av_frame_unref(frame);
  176. return AVERROR(ENOMEM);
  177. }
  178. static int get_audio_buffer(AVFrame *frame, int align)
  179. {
  180. int channels;
  181. int planar = av_sample_fmt_is_planar(frame->format);
  182. int planes;
  183. int ret, i;
  184. if (!frame->channels)
  185. frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  186. channels = frame->channels;
  187. planes = planar ? channels : 1;
  188. CHECK_CHANNELS_CONSISTENCY(frame);
  189. if (!frame->linesize[0]) {
  190. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  191. frame->nb_samples, frame->format,
  192. align);
  193. if (ret < 0)
  194. return ret;
  195. }
  196. if (planes > AV_NUM_DATA_POINTERS) {
  197. frame->extended_data = av_mallocz_array(planes,
  198. sizeof(*frame->extended_data));
  199. frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
  200. sizeof(*frame->extended_buf));
  201. if (!frame->extended_data || !frame->extended_buf) {
  202. av_freep(&frame->extended_data);
  203. av_freep(&frame->extended_buf);
  204. return AVERROR(ENOMEM);
  205. }
  206. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  207. } else
  208. frame->extended_data = frame->data;
  209. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  210. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  211. if (!frame->buf[i]) {
  212. av_frame_unref(frame);
  213. return AVERROR(ENOMEM);
  214. }
  215. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  216. }
  217. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  218. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  219. if (!frame->extended_buf[i]) {
  220. av_frame_unref(frame);
  221. return AVERROR(ENOMEM);
  222. }
  223. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  224. }
  225. return 0;
  226. }
  227. int av_frame_get_buffer(AVFrame *frame, int align)
  228. {
  229. if (frame->format < 0)
  230. return AVERROR(EINVAL);
  231. if (frame->width > 0 && frame->height > 0)
  232. return get_video_buffer(frame, align);
  233. else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
  234. return get_audio_buffer(frame, align);
  235. return AVERROR(EINVAL);
  236. }
  237. static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
  238. {
  239. int i;
  240. dst->key_frame = src->key_frame;
  241. dst->pict_type = src->pict_type;
  242. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  243. dst->pts = src->pts;
  244. dst->repeat_pict = src->repeat_pict;
  245. dst->interlaced_frame = src->interlaced_frame;
  246. dst->top_field_first = src->top_field_first;
  247. dst->palette_has_changed = src->palette_has_changed;
  248. dst->sample_rate = src->sample_rate;
  249. dst->opaque = src->opaque;
  250. dst->pkt_pts = src->pkt_pts;
  251. dst->pkt_dts = src->pkt_dts;
  252. dst->pkt_pos = src->pkt_pos;
  253. dst->pkt_size = src->pkt_size;
  254. dst->pkt_duration = src->pkt_duration;
  255. dst->reordered_opaque = src->reordered_opaque;
  256. dst->quality = src->quality;
  257. dst->best_effort_timestamp = src->best_effort_timestamp;
  258. dst->coded_picture_number = src->coded_picture_number;
  259. dst->display_picture_number = src->display_picture_number;
  260. dst->flags = src->flags;
  261. dst->decode_error_flags = src->decode_error_flags;
  262. dst->color_primaries = src->color_primaries;
  263. dst->color_trc = src->color_trc;
  264. dst->colorspace = src->colorspace;
  265. dst->color_range = src->color_range;
  266. dst->chroma_location = src->chroma_location;
  267. av_dict_copy(&dst->metadata, src->metadata, 0);
  268. #if FF_API_ERROR_FRAME
  269. FF_DISABLE_DEPRECATION_WARNINGS
  270. memcpy(dst->error, src->error, sizeof(dst->error));
  271. FF_ENABLE_DEPRECATION_WARNINGS
  272. #endif
  273. for (i = 0; i < src->nb_side_data; i++) {
  274. const AVFrameSideData *sd_src = src->side_data[i];
  275. AVFrameSideData *sd_dst;
  276. if ( sd_src->type == AV_FRAME_DATA_PANSCAN
  277. && (src->width != dst->width || src->height != dst->height))
  278. continue;
  279. if (force_copy) {
  280. sd_dst = av_frame_new_side_data(dst, sd_src->type,
  281. sd_src->size);
  282. if (!sd_dst) {
  283. wipe_side_data(dst);
  284. return AVERROR(ENOMEM);
  285. }
  286. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  287. } else {
  288. sd_dst = av_frame_new_side_data(dst, sd_src->type, 0);
  289. if (!sd_dst) {
  290. wipe_side_data(dst);
  291. return AVERROR(ENOMEM);
  292. }
  293. sd_dst->buf = av_buffer_ref(sd_src->buf);
  294. if (!sd_dst->buf) {
  295. wipe_side_data(dst);
  296. return AVERROR(ENOMEM);
  297. }
  298. sd_dst->data = sd_dst->buf->data;
  299. sd_dst->size = sd_dst->buf->size;
  300. }
  301. av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
  302. }
  303. #if FF_API_FRAME_QP
  304. FF_DISABLE_DEPRECATION_WARNINGS
  305. dst->qscale_table = NULL;
  306. dst->qstride = 0;
  307. dst->qscale_type = 0;
  308. if (src->qp_table_buf) {
  309. dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
  310. if (dst->qp_table_buf) {
  311. dst->qscale_table = dst->qp_table_buf->data;
  312. dst->qstride = src->qstride;
  313. dst->qscale_type = src->qscale_type;
  314. }
  315. }
  316. FF_ENABLE_DEPRECATION_WARNINGS
  317. #endif
  318. return 0;
  319. }
  320. int av_frame_ref(AVFrame *dst, const AVFrame *src)
  321. {
  322. int i, ret = 0;
  323. dst->format = src->format;
  324. dst->width = src->width;
  325. dst->height = src->height;
  326. dst->channels = src->channels;
  327. dst->channel_layout = src->channel_layout;
  328. dst->nb_samples = src->nb_samples;
  329. ret = frame_copy_props(dst, src, 0);
  330. if (ret < 0)
  331. return ret;
  332. /* duplicate the frame data if it's not refcounted */
  333. if (!src->buf[0]) {
  334. ret = av_frame_get_buffer(dst, 32);
  335. if (ret < 0)
  336. return ret;
  337. ret = av_frame_copy(dst, src);
  338. if (ret < 0)
  339. av_frame_unref(dst);
  340. return ret;
  341. }
  342. /* ref the buffers */
  343. for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
  344. if (!src->buf[i])
  345. continue;
  346. dst->buf[i] = av_buffer_ref(src->buf[i]);
  347. if (!dst->buf[i]) {
  348. ret = AVERROR(ENOMEM);
  349. goto fail;
  350. }
  351. }
  352. if (src->extended_buf) {
  353. dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
  354. src->nb_extended_buf);
  355. if (!dst->extended_buf) {
  356. ret = AVERROR(ENOMEM);
  357. goto fail;
  358. }
  359. dst->nb_extended_buf = src->nb_extended_buf;
  360. for (i = 0; i < src->nb_extended_buf; i++) {
  361. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  362. if (!dst->extended_buf[i]) {
  363. ret = AVERROR(ENOMEM);
  364. goto fail;
  365. }
  366. }
  367. }
  368. /* duplicate extended data */
  369. if (src->extended_data != src->data) {
  370. int ch = src->channels;
  371. if (!ch) {
  372. ret = AVERROR(EINVAL);
  373. goto fail;
  374. }
  375. CHECK_CHANNELS_CONSISTENCY(src);
  376. dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
  377. if (!dst->extended_data) {
  378. ret = AVERROR(ENOMEM);
  379. goto fail;
  380. }
  381. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  382. } else
  383. dst->extended_data = dst->data;
  384. memcpy(dst->data, src->data, sizeof(src->data));
  385. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  386. return 0;
  387. fail:
  388. av_frame_unref(dst);
  389. return ret;
  390. }
  391. AVFrame *av_frame_clone(const AVFrame *src)
  392. {
  393. AVFrame *ret = av_frame_alloc();
  394. if (!ret)
  395. return NULL;
  396. if (av_frame_ref(ret, src) < 0)
  397. av_frame_free(&ret);
  398. return ret;
  399. }
  400. void av_frame_unref(AVFrame *frame)
  401. {
  402. int i;
  403. if (!frame)
  404. return;
  405. wipe_side_data(frame);
  406. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  407. av_buffer_unref(&frame->buf[i]);
  408. for (i = 0; i < frame->nb_extended_buf; i++)
  409. av_buffer_unref(&frame->extended_buf[i]);
  410. av_freep(&frame->extended_buf);
  411. av_dict_free(&frame->metadata);
  412. #if FF_API_FRAME_QP
  413. av_buffer_unref(&frame->qp_table_buf);
  414. #endif
  415. get_frame_defaults(frame);
  416. }
  417. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  418. {
  419. *dst = *src;
  420. if (src->extended_data == src->data)
  421. dst->extended_data = dst->data;
  422. memset(src, 0, sizeof(*src));
  423. get_frame_defaults(src);
  424. }
  425. int av_frame_is_writable(AVFrame *frame)
  426. {
  427. int i, ret = 1;
  428. /* assume non-refcounted frames are not writable */
  429. if (!frame->buf[0])
  430. return 0;
  431. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  432. if (frame->buf[i])
  433. ret &= !!av_buffer_is_writable(frame->buf[i]);
  434. for (i = 0; i < frame->nb_extended_buf; i++)
  435. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  436. return ret;
  437. }
  438. int av_frame_make_writable(AVFrame *frame)
  439. {
  440. AVFrame tmp;
  441. int ret;
  442. if (!frame->buf[0])
  443. return AVERROR(EINVAL);
  444. if (av_frame_is_writable(frame))
  445. return 0;
  446. memset(&tmp, 0, sizeof(tmp));
  447. tmp.format = frame->format;
  448. tmp.width = frame->width;
  449. tmp.height = frame->height;
  450. tmp.channels = frame->channels;
  451. tmp.channel_layout = frame->channel_layout;
  452. tmp.nb_samples = frame->nb_samples;
  453. ret = av_frame_get_buffer(&tmp, 32);
  454. if (ret < 0)
  455. return ret;
  456. ret = av_frame_copy(&tmp, frame);
  457. if (ret < 0) {
  458. av_frame_unref(&tmp);
  459. return ret;
  460. }
  461. ret = av_frame_copy_props(&tmp, frame);
  462. if (ret < 0) {
  463. av_frame_unref(&tmp);
  464. return ret;
  465. }
  466. av_frame_unref(frame);
  467. *frame = tmp;
  468. if (tmp.data == tmp.extended_data)
  469. frame->extended_data = frame->data;
  470. return 0;
  471. }
  472. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  473. {
  474. return frame_copy_props(dst, src, 1);
  475. }
  476. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  477. {
  478. uint8_t *data;
  479. int planes, i;
  480. if (frame->nb_samples) {
  481. int channels = frame->channels;
  482. if (!channels)
  483. return NULL;
  484. CHECK_CHANNELS_CONSISTENCY(frame);
  485. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  486. } else
  487. planes = 4;
  488. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  489. return NULL;
  490. data = frame->extended_data[plane];
  491. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  492. AVBufferRef *buf = frame->buf[i];
  493. if (data >= buf->data && data < buf->data + buf->size)
  494. return buf;
  495. }
  496. for (i = 0; i < frame->nb_extended_buf; i++) {
  497. AVBufferRef *buf = frame->extended_buf[i];
  498. if (data >= buf->data && data < buf->data + buf->size)
  499. return buf;
  500. }
  501. return NULL;
  502. }
  503. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  504. enum AVFrameSideDataType type,
  505. int size)
  506. {
  507. AVFrameSideData *ret, **tmp;
  508. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  509. return NULL;
  510. tmp = av_realloc(frame->side_data,
  511. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  512. if (!tmp)
  513. return NULL;
  514. frame->side_data = tmp;
  515. ret = av_mallocz(sizeof(*ret));
  516. if (!ret)
  517. return NULL;
  518. if (size > 0) {
  519. ret->buf = av_buffer_alloc(size);
  520. if (!ret->buf) {
  521. av_freep(&ret);
  522. return NULL;
  523. }
  524. ret->data = ret->buf->data;
  525. ret->size = size;
  526. }
  527. ret->type = type;
  528. frame->side_data[frame->nb_side_data++] = ret;
  529. return ret;
  530. }
  531. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  532. enum AVFrameSideDataType type)
  533. {
  534. int i;
  535. for (i = 0; i < frame->nb_side_data; i++) {
  536. if (frame->side_data[i]->type == type)
  537. return frame->side_data[i];
  538. }
  539. return NULL;
  540. }
  541. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  542. {
  543. const uint8_t *src_data[4];
  544. int i, planes;
  545. if (dst->width < src->width ||
  546. dst->height < src->height)
  547. return AVERROR(EINVAL);
  548. planes = av_pix_fmt_count_planes(dst->format);
  549. for (i = 0; i < planes; i++)
  550. if (!dst->data[i] || !src->data[i])
  551. return AVERROR(EINVAL);
  552. memcpy(src_data, src->data, sizeof(src_data));
  553. av_image_copy(dst->data, dst->linesize,
  554. src_data, src->linesize,
  555. dst->format, src->width, src->height);
  556. return 0;
  557. }
  558. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  559. {
  560. int planar = av_sample_fmt_is_planar(dst->format);
  561. int channels = dst->channels;
  562. int planes = planar ? channels : 1;
  563. int i;
  564. if (dst->nb_samples != src->nb_samples ||
  565. dst->channels != src->channels ||
  566. dst->channel_layout != src->channel_layout)
  567. return AVERROR(EINVAL);
  568. CHECK_CHANNELS_CONSISTENCY(src);
  569. for (i = 0; i < planes; i++)
  570. if (!dst->extended_data[i] || !src->extended_data[i])
  571. return AVERROR(EINVAL);
  572. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  573. dst->nb_samples, channels, dst->format);
  574. return 0;
  575. }
  576. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  577. {
  578. if (dst->format != src->format || dst->format < 0)
  579. return AVERROR(EINVAL);
  580. if (dst->width > 0 && dst->height > 0)
  581. return frame_copy_video(dst, src);
  582. else if (dst->nb_samples > 0 && dst->channel_layout)
  583. return frame_copy_audio(dst, src);
  584. return AVERROR(EINVAL);
  585. }
  586. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  587. {
  588. int i;
  589. for (i = 0; i < frame->nb_side_data; i++) {
  590. AVFrameSideData *sd = frame->side_data[i];
  591. if (sd->type == type) {
  592. free_side_data(&frame->side_data[i]);
  593. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  594. frame->nb_side_data--;
  595. }
  596. }
  597. }
  598. const char *av_frame_side_data_name(enum AVFrameSideDataType type)
  599. {
  600. switch(type) {
  601. case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
  602. case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
  603. case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
  604. case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
  605. case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
  606. case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
  607. case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
  608. case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
  609. }
  610. return NULL;
  611. }