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.

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