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.

750 lines
22KB

  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. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  28. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  29. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  30. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  31. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  32. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  33. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  34. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  35. MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
  36. MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
  37. MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
  38. #define CHECK_CHANNELS_CONSISTENCY(frame) \
  39. av_assert2(!(frame)->channel_layout || \
  40. (frame)->channels == \
  41. av_get_channel_layout_nb_channels((frame)->channel_layout))
  42. AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
  43. #if FF_API_FRAME_QP
  44. int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
  45. {
  46. av_buffer_unref(&f->qp_table_buf);
  47. f->qp_table_buf = buf;
  48. FF_DISABLE_DEPRECATION_WARNINGS
  49. f->qscale_table = buf->data;
  50. f->qstride = stride;
  51. f->qscale_type = qp_type;
  52. FF_ENABLE_DEPRECATION_WARNINGS
  53. return 0;
  54. }
  55. int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
  56. {
  57. FF_DISABLE_DEPRECATION_WARNINGS
  58. *stride = f->qstride;
  59. *type = f->qscale_type;
  60. FF_ENABLE_DEPRECATION_WARNINGS
  61. if (!f->qp_table_buf)
  62. return NULL;
  63. return f->qp_table_buf->data;
  64. }
  65. #endif
  66. const char *av_get_colorspace_name(enum AVColorSpace val)
  67. {
  68. static const char * const name[] = {
  69. [AVCOL_SPC_RGB] = "GBR",
  70. [AVCOL_SPC_BT709] = "bt709",
  71. [AVCOL_SPC_FCC] = "fcc",
  72. [AVCOL_SPC_BT470BG] = "bt470bg",
  73. [AVCOL_SPC_SMPTE170M] = "smpte170m",
  74. [AVCOL_SPC_SMPTE240M] = "smpte240m",
  75. [AVCOL_SPC_YCOCG] = "YCgCo",
  76. };
  77. if ((unsigned)val >= FF_ARRAY_ELEMS(name))
  78. return NULL;
  79. return name[val];
  80. }
  81. static void get_frame_defaults(AVFrame *frame)
  82. {
  83. if (frame->extended_data != frame->data)
  84. av_freep(&frame->extended_data);
  85. memset(frame, 0, sizeof(*frame));
  86. frame->pts =
  87. frame->pkt_dts =
  88. frame->pkt_pts = AV_NOPTS_VALUE;
  89. av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
  90. av_frame_set_pkt_duration (frame, 0);
  91. av_frame_set_pkt_pos (frame, -1);
  92. av_frame_set_pkt_size (frame, -1);
  93. frame->key_frame = 1;
  94. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  95. frame->format = -1; /* unknown */
  96. frame->extended_data = frame->data;
  97. frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
  98. frame->color_trc = AVCOL_TRC_UNSPECIFIED;
  99. frame->colorspace = AVCOL_SPC_UNSPECIFIED;
  100. frame->color_range = AVCOL_RANGE_UNSPECIFIED;
  101. frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  102. }
  103. static void free_side_data(AVFrameSideData **ptr_sd)
  104. {
  105. AVFrameSideData *sd = *ptr_sd;
  106. av_buffer_unref(&sd->buf);
  107. av_dict_free(&sd->metadata);
  108. av_freep(ptr_sd);
  109. }
  110. static void wipe_side_data(AVFrame *frame)
  111. {
  112. int i;
  113. for (i = 0; i < frame->nb_side_data; i++) {
  114. free_side_data(&frame->side_data[i]);
  115. }
  116. frame->nb_side_data = 0;
  117. av_freep(&frame->side_data);
  118. }
  119. AVFrame *av_frame_alloc(void)
  120. {
  121. AVFrame *frame = av_mallocz(sizeof(*frame));
  122. if (!frame)
  123. return NULL;
  124. frame->extended_data = NULL;
  125. get_frame_defaults(frame);
  126. return frame;
  127. }
  128. void av_frame_free(AVFrame **frame)
  129. {
  130. if (!frame || !*frame)
  131. return;
  132. av_frame_unref(*frame);
  133. av_freep(frame);
  134. }
  135. static int get_video_buffer(AVFrame *frame, int align)
  136. {
  137. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  138. int ret, i;
  139. if (!desc)
  140. return AVERROR(EINVAL);
  141. if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
  142. return ret;
  143. if (!frame->linesize[0]) {
  144. for(i=1; i<=align; i+=i) {
  145. ret = av_image_fill_linesizes(frame->linesize, frame->format,
  146. FFALIGN(frame->width, i));
  147. if (ret < 0)
  148. return ret;
  149. if (!(frame->linesize[0] & (align-1)))
  150. break;
  151. }
  152. for (i = 0; i < 4 && frame->linesize[i]; i++)
  153. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  154. }
  155. for (i = 0; i < 4 && frame->linesize[i]; i++) {
  156. int h = FFALIGN(frame->height, 32);
  157. if (i == 1 || i == 2)
  158. h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
  159. frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
  160. if (!frame->buf[i])
  161. goto fail;
  162. frame->data[i] = frame->buf[i]->data;
  163. }
  164. if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  165. av_buffer_unref(&frame->buf[1]);
  166. frame->buf[1] = av_buffer_alloc(AVPALETTE_SIZE);
  167. if (!frame->buf[1])
  168. goto fail;
  169. frame->data[1] = frame->buf[1]->data;
  170. }
  171. frame->extended_data = frame->data;
  172. return 0;
  173. fail:
  174. av_frame_unref(frame);
  175. return AVERROR(ENOMEM);
  176. }
  177. static int get_audio_buffer(AVFrame *frame, int align)
  178. {
  179. int channels;
  180. int planar = av_sample_fmt_is_planar(frame->format);
  181. int planes;
  182. int ret, i;
  183. if (!frame->channels)
  184. frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  185. channels = frame->channels;
  186. planes = planar ? channels : 1;
  187. CHECK_CHANNELS_CONSISTENCY(frame);
  188. if (!frame->linesize[0]) {
  189. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  190. frame->nb_samples, frame->format,
  191. align);
  192. if (ret < 0)
  193. return ret;
  194. }
  195. if (planes > AV_NUM_DATA_POINTERS) {
  196. frame->extended_data = av_mallocz_array(planes,
  197. sizeof(*frame->extended_data));
  198. frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
  199. sizeof(*frame->extended_buf));
  200. if (!frame->extended_data || !frame->extended_buf) {
  201. av_freep(&frame->extended_data);
  202. av_freep(&frame->extended_buf);
  203. return AVERROR(ENOMEM);
  204. }
  205. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  206. } else
  207. frame->extended_data = frame->data;
  208. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  209. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  210. if (!frame->buf[i]) {
  211. av_frame_unref(frame);
  212. return AVERROR(ENOMEM);
  213. }
  214. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  215. }
  216. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  217. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  218. if (!frame->extended_buf[i]) {
  219. av_frame_unref(frame);
  220. return AVERROR(ENOMEM);
  221. }
  222. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  223. }
  224. return 0;
  225. }
  226. int av_frame_get_buffer(AVFrame *frame, int align)
  227. {
  228. if (frame->format < 0)
  229. return AVERROR(EINVAL);
  230. if (frame->width > 0 && frame->height > 0)
  231. return get_video_buffer(frame, align);
  232. else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
  233. return get_audio_buffer(frame, align);
  234. return AVERROR(EINVAL);
  235. }
  236. static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
  237. {
  238. int i;
  239. dst->key_frame = src->key_frame;
  240. dst->pict_type = src->pict_type;
  241. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  242. dst->pts = src->pts;
  243. dst->repeat_pict = src->repeat_pict;
  244. dst->interlaced_frame = src->interlaced_frame;
  245. dst->top_field_first = src->top_field_first;
  246. dst->palette_has_changed = src->palette_has_changed;
  247. dst->sample_rate = src->sample_rate;
  248. dst->opaque = src->opaque;
  249. dst->pkt_pts = src->pkt_pts;
  250. dst->pkt_dts = src->pkt_dts;
  251. dst->pkt_pos = src->pkt_pos;
  252. dst->pkt_size = src->pkt_size;
  253. dst->pkt_duration = src->pkt_duration;
  254. dst->reordered_opaque = src->reordered_opaque;
  255. dst->quality = src->quality;
  256. dst->best_effort_timestamp = src->best_effort_timestamp;
  257. dst->coded_picture_number = src->coded_picture_number;
  258. dst->display_picture_number = src->display_picture_number;
  259. dst->flags = src->flags;
  260. dst->decode_error_flags = src->decode_error_flags;
  261. dst->color_primaries = src->color_primaries;
  262. dst->color_trc = src->color_trc;
  263. dst->colorspace = src->colorspace;
  264. dst->color_range = src->color_range;
  265. dst->chroma_location = src->chroma_location;
  266. av_dict_copy(&dst->metadata, src->metadata, 0);
  267. #if FF_API_ERROR_FRAME
  268. FF_DISABLE_DEPRECATION_WARNINGS
  269. memcpy(dst->error, src->error, sizeof(dst->error));
  270. FF_ENABLE_DEPRECATION_WARNINGS
  271. #endif
  272. for (i = 0; i < src->nb_side_data; i++) {
  273. const AVFrameSideData *sd_src = src->side_data[i];
  274. AVFrameSideData *sd_dst;
  275. if ( sd_src->type == AV_FRAME_DATA_PANSCAN
  276. && (src->width != dst->width || src->height != dst->height))
  277. continue;
  278. if (force_copy) {
  279. sd_dst = av_frame_new_side_data(dst, sd_src->type,
  280. sd_src->size);
  281. if (!sd_dst) {
  282. wipe_side_data(dst);
  283. return AVERROR(ENOMEM);
  284. }
  285. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  286. } else {
  287. sd_dst = av_frame_new_side_data(dst, sd_src->type, 0);
  288. if (!sd_dst) {
  289. wipe_side_data(dst);
  290. return AVERROR(ENOMEM);
  291. }
  292. sd_dst->buf = av_buffer_ref(sd_src->buf);
  293. if (!sd_dst->buf) {
  294. wipe_side_data(dst);
  295. return AVERROR(ENOMEM);
  296. }
  297. sd_dst->data = sd_dst->buf->data;
  298. sd_dst->size = sd_dst->buf->size;
  299. }
  300. av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
  301. }
  302. #if FF_API_FRAME_QP
  303. FF_DISABLE_DEPRECATION_WARNINGS
  304. dst->qscale_table = NULL;
  305. dst->qstride = 0;
  306. dst->qscale_type = 0;
  307. av_buffer_unref(&dst->qp_table_buf);
  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. if (src->hw_frames_ctx) {
  369. dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
  370. if (!dst->hw_frames_ctx) {
  371. ret = AVERROR(ENOMEM);
  372. goto fail;
  373. }
  374. }
  375. /* duplicate extended data */
  376. if (src->extended_data != src->data) {
  377. int ch = src->channels;
  378. if (!ch) {
  379. ret = AVERROR(EINVAL);
  380. goto fail;
  381. }
  382. CHECK_CHANNELS_CONSISTENCY(src);
  383. dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
  384. if (!dst->extended_data) {
  385. ret = AVERROR(ENOMEM);
  386. goto fail;
  387. }
  388. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  389. } else
  390. dst->extended_data = dst->data;
  391. memcpy(dst->data, src->data, sizeof(src->data));
  392. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  393. return 0;
  394. fail:
  395. av_frame_unref(dst);
  396. return ret;
  397. }
  398. AVFrame *av_frame_clone(const AVFrame *src)
  399. {
  400. AVFrame *ret = av_frame_alloc();
  401. if (!ret)
  402. return NULL;
  403. if (av_frame_ref(ret, src) < 0)
  404. av_frame_free(&ret);
  405. return ret;
  406. }
  407. void av_frame_unref(AVFrame *frame)
  408. {
  409. int i;
  410. if (!frame)
  411. return;
  412. wipe_side_data(frame);
  413. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  414. av_buffer_unref(&frame->buf[i]);
  415. for (i = 0; i < frame->nb_extended_buf; i++)
  416. av_buffer_unref(&frame->extended_buf[i]);
  417. av_freep(&frame->extended_buf);
  418. av_dict_free(&frame->metadata);
  419. #if FF_API_FRAME_QP
  420. av_buffer_unref(&frame->qp_table_buf);
  421. #endif
  422. av_buffer_unref(&frame->hw_frames_ctx);
  423. get_frame_defaults(frame);
  424. }
  425. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  426. {
  427. *dst = *src;
  428. if (src->extended_data == src->data)
  429. dst->extended_data = dst->data;
  430. memset(src, 0, sizeof(*src));
  431. get_frame_defaults(src);
  432. }
  433. int av_frame_is_writable(AVFrame *frame)
  434. {
  435. int i, ret = 1;
  436. /* assume non-refcounted frames are not writable */
  437. if (!frame->buf[0])
  438. return 0;
  439. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  440. if (frame->buf[i])
  441. ret &= !!av_buffer_is_writable(frame->buf[i]);
  442. for (i = 0; i < frame->nb_extended_buf; i++)
  443. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  444. return ret;
  445. }
  446. int av_frame_make_writable(AVFrame *frame)
  447. {
  448. AVFrame tmp;
  449. int ret;
  450. if (!frame->buf[0])
  451. return AVERROR(EINVAL);
  452. if (av_frame_is_writable(frame))
  453. return 0;
  454. memset(&tmp, 0, sizeof(tmp));
  455. tmp.format = frame->format;
  456. tmp.width = frame->width;
  457. tmp.height = frame->height;
  458. tmp.channels = frame->channels;
  459. tmp.channel_layout = frame->channel_layout;
  460. tmp.nb_samples = frame->nb_samples;
  461. ret = av_frame_get_buffer(&tmp, 32);
  462. if (ret < 0)
  463. return ret;
  464. ret = av_frame_copy(&tmp, frame);
  465. if (ret < 0) {
  466. av_frame_unref(&tmp);
  467. return ret;
  468. }
  469. ret = av_frame_copy_props(&tmp, frame);
  470. if (ret < 0) {
  471. av_frame_unref(&tmp);
  472. return ret;
  473. }
  474. av_frame_unref(frame);
  475. *frame = tmp;
  476. if (tmp.data == tmp.extended_data)
  477. frame->extended_data = frame->data;
  478. return 0;
  479. }
  480. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  481. {
  482. return frame_copy_props(dst, src, 1);
  483. }
  484. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  485. {
  486. uint8_t *data;
  487. int planes, i;
  488. if (frame->nb_samples) {
  489. int channels = frame->channels;
  490. if (!channels)
  491. return NULL;
  492. CHECK_CHANNELS_CONSISTENCY(frame);
  493. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  494. } else
  495. planes = 4;
  496. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  497. return NULL;
  498. data = frame->extended_data[plane];
  499. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  500. AVBufferRef *buf = frame->buf[i];
  501. if (data >= buf->data && data < buf->data + buf->size)
  502. return buf;
  503. }
  504. for (i = 0; i < frame->nb_extended_buf; i++) {
  505. AVBufferRef *buf = frame->extended_buf[i];
  506. if (data >= buf->data && data < buf->data + buf->size)
  507. return buf;
  508. }
  509. return NULL;
  510. }
  511. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  512. enum AVFrameSideDataType type,
  513. int size)
  514. {
  515. AVFrameSideData *ret, **tmp;
  516. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  517. return NULL;
  518. tmp = av_realloc(frame->side_data,
  519. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  520. if (!tmp)
  521. return NULL;
  522. frame->side_data = tmp;
  523. ret = av_mallocz(sizeof(*ret));
  524. if (!ret)
  525. return NULL;
  526. if (size > 0) {
  527. ret->buf = av_buffer_alloc(size);
  528. if (!ret->buf) {
  529. av_freep(&ret);
  530. return NULL;
  531. }
  532. ret->data = ret->buf->data;
  533. ret->size = size;
  534. }
  535. ret->type = type;
  536. frame->side_data[frame->nb_side_data++] = ret;
  537. return ret;
  538. }
  539. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  540. enum AVFrameSideDataType type)
  541. {
  542. int i;
  543. for (i = 0; i < frame->nb_side_data; i++) {
  544. if (frame->side_data[i]->type == type)
  545. return frame->side_data[i];
  546. }
  547. return NULL;
  548. }
  549. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  550. {
  551. const uint8_t *src_data[4];
  552. int i, planes;
  553. if (dst->width < src->width ||
  554. dst->height < src->height)
  555. return AVERROR(EINVAL);
  556. planes = av_pix_fmt_count_planes(dst->format);
  557. for (i = 0; i < planes; i++)
  558. if (!dst->data[i] || !src->data[i])
  559. return AVERROR(EINVAL);
  560. memcpy(src_data, src->data, sizeof(src_data));
  561. av_image_copy(dst->data, dst->linesize,
  562. src_data, src->linesize,
  563. dst->format, src->width, src->height);
  564. return 0;
  565. }
  566. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  567. {
  568. int planar = av_sample_fmt_is_planar(dst->format);
  569. int channels = dst->channels;
  570. int planes = planar ? channels : 1;
  571. int i;
  572. if (dst->nb_samples != src->nb_samples ||
  573. dst->channels != src->channels ||
  574. dst->channel_layout != src->channel_layout)
  575. return AVERROR(EINVAL);
  576. CHECK_CHANNELS_CONSISTENCY(src);
  577. for (i = 0; i < planes; i++)
  578. if (!dst->extended_data[i] || !src->extended_data[i])
  579. return AVERROR(EINVAL);
  580. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  581. dst->nb_samples, channels, dst->format);
  582. return 0;
  583. }
  584. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  585. {
  586. if (dst->format != src->format || dst->format < 0)
  587. return AVERROR(EINVAL);
  588. if (dst->width > 0 && dst->height > 0)
  589. return frame_copy_video(dst, src);
  590. else if (dst->nb_samples > 0 && dst->channel_layout)
  591. return frame_copy_audio(dst, src);
  592. return AVERROR(EINVAL);
  593. }
  594. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  595. {
  596. int i;
  597. for (i = 0; i < frame->nb_side_data; i++) {
  598. AVFrameSideData *sd = frame->side_data[i];
  599. if (sd->type == type) {
  600. free_side_data(&frame->side_data[i]);
  601. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  602. frame->nb_side_data--;
  603. }
  604. }
  605. }
  606. const char *av_frame_side_data_name(enum AVFrameSideDataType type)
  607. {
  608. switch(type) {
  609. case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
  610. case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
  611. case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
  612. case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
  613. case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
  614. case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
  615. case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
  616. case AV_FRAME_DATA_AFD: return "Active format description";
  617. case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
  618. case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
  619. case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
  620. case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
  621. case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";
  622. }
  623. return NULL;
  624. }