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.

731 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. memcpy(dst->error, src->error, sizeof(dst->error));
  269. for (i = 0; i < src->nb_side_data; i++) {
  270. const AVFrameSideData *sd_src = src->side_data[i];
  271. AVFrameSideData *sd_dst;
  272. if ( sd_src->type == AV_FRAME_DATA_PANSCAN
  273. && (src->width != dst->width || src->height != dst->height))
  274. continue;
  275. if (force_copy) {
  276. sd_dst = av_frame_new_side_data(dst, sd_src->type,
  277. sd_src->size);
  278. if (!sd_dst) {
  279. wipe_side_data(dst);
  280. return AVERROR(ENOMEM);
  281. }
  282. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  283. } else {
  284. sd_dst = av_frame_new_side_data(dst, sd_src->type, 0);
  285. if (!sd_dst) {
  286. wipe_side_data(dst);
  287. return AVERROR(ENOMEM);
  288. }
  289. sd_dst->buf = av_buffer_ref(sd_src->buf);
  290. if (!sd_dst->buf) {
  291. wipe_side_data(dst);
  292. return AVERROR(ENOMEM);
  293. }
  294. sd_dst->data = sd_dst->buf->data;
  295. sd_dst->size = sd_dst->buf->size;
  296. }
  297. av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
  298. }
  299. #if FF_API_FRAME_QP
  300. FF_DISABLE_DEPRECATION_WARNINGS
  301. dst->qscale_table = NULL;
  302. dst->qstride = 0;
  303. dst->qscale_type = 0;
  304. if (src->qp_table_buf) {
  305. dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
  306. if (dst->qp_table_buf) {
  307. dst->qscale_table = dst->qp_table_buf->data;
  308. dst->qstride = src->qstride;
  309. dst->qscale_type = src->qscale_type;
  310. }
  311. }
  312. FF_ENABLE_DEPRECATION_WARNINGS
  313. #endif
  314. return 0;
  315. }
  316. int av_frame_ref(AVFrame *dst, const AVFrame *src)
  317. {
  318. int i, ret = 0;
  319. dst->format = src->format;
  320. dst->width = src->width;
  321. dst->height = src->height;
  322. dst->channels = src->channels;
  323. dst->channel_layout = src->channel_layout;
  324. dst->nb_samples = src->nb_samples;
  325. ret = frame_copy_props(dst, src, 0);
  326. if (ret < 0)
  327. return ret;
  328. /* duplicate the frame data if it's not refcounted */
  329. if (!src->buf[0]) {
  330. ret = av_frame_get_buffer(dst, 32);
  331. if (ret < 0)
  332. return ret;
  333. ret = av_frame_copy(dst, src);
  334. if (ret < 0)
  335. av_frame_unref(dst);
  336. return ret;
  337. }
  338. /* ref the buffers */
  339. for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
  340. if (!src->buf[i])
  341. continue;
  342. dst->buf[i] = av_buffer_ref(src->buf[i]);
  343. if (!dst->buf[i]) {
  344. ret = AVERROR(ENOMEM);
  345. goto fail;
  346. }
  347. }
  348. if (src->extended_buf) {
  349. dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
  350. src->nb_extended_buf);
  351. if (!dst->extended_buf) {
  352. ret = AVERROR(ENOMEM);
  353. goto fail;
  354. }
  355. dst->nb_extended_buf = src->nb_extended_buf;
  356. for (i = 0; i < src->nb_extended_buf; i++) {
  357. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  358. if (!dst->extended_buf[i]) {
  359. ret = AVERROR(ENOMEM);
  360. goto fail;
  361. }
  362. }
  363. }
  364. /* duplicate extended data */
  365. if (src->extended_data != src->data) {
  366. int ch = src->channels;
  367. if (!ch) {
  368. ret = AVERROR(EINVAL);
  369. goto fail;
  370. }
  371. CHECK_CHANNELS_CONSISTENCY(src);
  372. dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
  373. if (!dst->extended_data) {
  374. ret = AVERROR(ENOMEM);
  375. goto fail;
  376. }
  377. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  378. } else
  379. dst->extended_data = dst->data;
  380. memcpy(dst->data, src->data, sizeof(src->data));
  381. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  382. return 0;
  383. fail:
  384. av_frame_unref(dst);
  385. return ret;
  386. }
  387. AVFrame *av_frame_clone(const AVFrame *src)
  388. {
  389. AVFrame *ret = av_frame_alloc();
  390. if (!ret)
  391. return NULL;
  392. if (av_frame_ref(ret, src) < 0)
  393. av_frame_free(&ret);
  394. return ret;
  395. }
  396. void av_frame_unref(AVFrame *frame)
  397. {
  398. int i;
  399. if (!frame)
  400. return;
  401. wipe_side_data(frame);
  402. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  403. av_buffer_unref(&frame->buf[i]);
  404. for (i = 0; i < frame->nb_extended_buf; i++)
  405. av_buffer_unref(&frame->extended_buf[i]);
  406. av_freep(&frame->extended_buf);
  407. av_dict_free(&frame->metadata);
  408. #if FF_API_FRAME_QP
  409. av_buffer_unref(&frame->qp_table_buf);
  410. #endif
  411. get_frame_defaults(frame);
  412. }
  413. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  414. {
  415. *dst = *src;
  416. if (src->extended_data == src->data)
  417. dst->extended_data = dst->data;
  418. memset(src, 0, sizeof(*src));
  419. get_frame_defaults(src);
  420. }
  421. int av_frame_is_writable(AVFrame *frame)
  422. {
  423. int i, ret = 1;
  424. /* assume non-refcounted frames are not writable */
  425. if (!frame->buf[0])
  426. return 0;
  427. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  428. if (frame->buf[i])
  429. ret &= !!av_buffer_is_writable(frame->buf[i]);
  430. for (i = 0; i < frame->nb_extended_buf; i++)
  431. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  432. return ret;
  433. }
  434. int av_frame_make_writable(AVFrame *frame)
  435. {
  436. AVFrame tmp;
  437. int ret;
  438. if (!frame->buf[0])
  439. return AVERROR(EINVAL);
  440. if (av_frame_is_writable(frame))
  441. return 0;
  442. memset(&tmp, 0, sizeof(tmp));
  443. tmp.format = frame->format;
  444. tmp.width = frame->width;
  445. tmp.height = frame->height;
  446. tmp.channels = frame->channels;
  447. tmp.channel_layout = frame->channel_layout;
  448. tmp.nb_samples = frame->nb_samples;
  449. ret = av_frame_get_buffer(&tmp, 32);
  450. if (ret < 0)
  451. return ret;
  452. ret = av_frame_copy(&tmp, frame);
  453. if (ret < 0) {
  454. av_frame_unref(&tmp);
  455. return ret;
  456. }
  457. ret = av_frame_copy_props(&tmp, frame);
  458. if (ret < 0) {
  459. av_frame_unref(&tmp);
  460. return ret;
  461. }
  462. av_frame_unref(frame);
  463. *frame = tmp;
  464. if (tmp.data == tmp.extended_data)
  465. frame->extended_data = frame->data;
  466. return 0;
  467. }
  468. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  469. {
  470. return frame_copy_props(dst, src, 1);
  471. }
  472. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  473. {
  474. uint8_t *data;
  475. int planes, i;
  476. if (frame->nb_samples) {
  477. int channels = frame->channels;
  478. if (!channels)
  479. return NULL;
  480. CHECK_CHANNELS_CONSISTENCY(frame);
  481. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  482. } else
  483. planes = 4;
  484. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  485. return NULL;
  486. data = frame->extended_data[plane];
  487. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  488. AVBufferRef *buf = frame->buf[i];
  489. if (data >= buf->data && data < buf->data + buf->size)
  490. return buf;
  491. }
  492. for (i = 0; i < frame->nb_extended_buf; i++) {
  493. AVBufferRef *buf = frame->extended_buf[i];
  494. if (data >= buf->data && data < buf->data + buf->size)
  495. return buf;
  496. }
  497. return NULL;
  498. }
  499. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  500. enum AVFrameSideDataType type,
  501. int size)
  502. {
  503. AVFrameSideData *ret, **tmp;
  504. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  505. return NULL;
  506. tmp = av_realloc(frame->side_data,
  507. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  508. if (!tmp)
  509. return NULL;
  510. frame->side_data = tmp;
  511. ret = av_mallocz(sizeof(*ret));
  512. if (!ret)
  513. return NULL;
  514. if (size > 0) {
  515. ret->buf = av_buffer_alloc(size);
  516. if (!ret->buf) {
  517. av_freep(&ret);
  518. return NULL;
  519. }
  520. ret->data = ret->buf->data;
  521. ret->size = size;
  522. }
  523. ret->type = type;
  524. frame->side_data[frame->nb_side_data++] = ret;
  525. return ret;
  526. }
  527. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  528. enum AVFrameSideDataType type)
  529. {
  530. int i;
  531. for (i = 0; i < frame->nb_side_data; i++) {
  532. if (frame->side_data[i]->type == type)
  533. return frame->side_data[i];
  534. }
  535. return NULL;
  536. }
  537. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  538. {
  539. const uint8_t *src_data[4];
  540. int i, planes;
  541. if (dst->width < src->width ||
  542. dst->height < src->height)
  543. return AVERROR(EINVAL);
  544. planes = av_pix_fmt_count_planes(dst->format);
  545. for (i = 0; i < planes; i++)
  546. if (!dst->data[i] || !src->data[i])
  547. return AVERROR(EINVAL);
  548. memcpy(src_data, src->data, sizeof(src_data));
  549. av_image_copy(dst->data, dst->linesize,
  550. src_data, src->linesize,
  551. dst->format, src->width, src->height);
  552. return 0;
  553. }
  554. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  555. {
  556. int planar = av_sample_fmt_is_planar(dst->format);
  557. int channels = dst->channels;
  558. int planes = planar ? channels : 1;
  559. int i;
  560. if (dst->nb_samples != src->nb_samples ||
  561. dst->channels != src->channels ||
  562. dst->channel_layout != src->channel_layout)
  563. return AVERROR(EINVAL);
  564. CHECK_CHANNELS_CONSISTENCY(src);
  565. for (i = 0; i < planes; i++)
  566. if (!dst->extended_data[i] || !src->extended_data[i])
  567. return AVERROR(EINVAL);
  568. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  569. dst->nb_samples, channels, dst->format);
  570. return 0;
  571. }
  572. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  573. {
  574. if (dst->format != src->format || dst->format < 0)
  575. return AVERROR(EINVAL);
  576. if (dst->width > 0 && dst->height > 0)
  577. return frame_copy_video(dst, src);
  578. else if (dst->nb_samples > 0 && dst->channel_layout)
  579. return frame_copy_audio(dst, src);
  580. return AVERROR(EINVAL);
  581. }
  582. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  583. {
  584. int i;
  585. for (i = 0; i < frame->nb_side_data; i++) {
  586. AVFrameSideData *sd = frame->side_data[i];
  587. if (sd->type == type) {
  588. free_side_data(&frame->side_data[i]);
  589. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  590. frame->nb_side_data--;
  591. }
  592. }
  593. }
  594. const char *av_frame_side_data_name(enum AVFrameSideDataType type)
  595. {
  596. switch(type) {
  597. case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
  598. case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
  599. case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
  600. case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
  601. case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
  602. case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
  603. case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
  604. case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
  605. }
  606. return NULL;
  607. }