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.

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