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.

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