frame.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. 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. f->qscale_table = buf->data;
  49. f->qstride = stride;
  50. f->qscale_type = qp_type;
  51. return 0;
  52. }
  53. int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
  54. {
  55. *stride = f->qstride;
  56. *type = f->qscale_type;
  57. if (!f->qp_table_buf)
  58. return NULL;
  59. return f->qp_table_buf->data;
  60. }
  61. const char *av_get_colorspace_name(enum AVColorSpace val)
  62. {
  63. static const char *name[] = {
  64. [AVCOL_SPC_RGB] = "GBR",
  65. [AVCOL_SPC_BT709] = "bt709",
  66. [AVCOL_SPC_FCC] = "fcc",
  67. [AVCOL_SPC_BT470BG] = "bt470bg",
  68. [AVCOL_SPC_SMPTE170M] = "smpte170m",
  69. [AVCOL_SPC_SMPTE240M] = "smpte240m",
  70. [AVCOL_SPC_YCOCG] = "YCgCo",
  71. };
  72. if ((unsigned)val >= FF_ARRAY_ELEMS(name))
  73. return NULL;
  74. return name[val];
  75. }
  76. static void get_frame_defaults(AVFrame *frame)
  77. {
  78. if (frame->extended_data != frame->data)
  79. av_freep(&frame->extended_data);
  80. memset(frame, 0, sizeof(*frame));
  81. frame->pts =
  82. frame->pkt_dts =
  83. frame->pkt_pts = AV_NOPTS_VALUE;
  84. av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
  85. av_frame_set_pkt_duration (frame, 0);
  86. av_frame_set_pkt_pos (frame, -1);
  87. av_frame_set_pkt_size (frame, -1);
  88. frame->key_frame = 1;
  89. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  90. frame->format = -1; /* unknown */
  91. frame->extended_data = frame->data;
  92. #if FF_API_AVFRAME_COLORSPACE
  93. frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
  94. frame->color_trc = AVCOL_TRC_UNSPECIFIED;
  95. frame->colorspace = AVCOL_SPC_UNSPECIFIED;
  96. frame->color_range = AVCOL_RANGE_UNSPECIFIED;
  97. frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  98. #endif
  99. }
  100. static void free_side_data(AVFrameSideData **ptr_sd)
  101. {
  102. AVFrameSideData *sd = *ptr_sd;
  103. av_freep(&sd->data);
  104. av_dict_free(&sd->metadata);
  105. av_freep(ptr_sd);
  106. }
  107. AVFrame *av_frame_alloc(void)
  108. {
  109. AVFrame *frame = av_mallocz(sizeof(*frame));
  110. if (!frame)
  111. return NULL;
  112. frame->extended_data = NULL;
  113. get_frame_defaults(frame);
  114. return frame;
  115. }
  116. void av_frame_free(AVFrame **frame)
  117. {
  118. if (!frame || !*frame)
  119. return;
  120. av_frame_unref(*frame);
  121. av_freep(frame);
  122. }
  123. static int get_video_buffer(AVFrame *frame, int align)
  124. {
  125. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  126. int ret, i;
  127. if (!desc)
  128. return AVERROR(EINVAL);
  129. if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
  130. return ret;
  131. if (!frame->linesize[0]) {
  132. for(i=1; i<=align; i+=i) {
  133. ret = av_image_fill_linesizes(frame->linesize, frame->format,
  134. FFALIGN(frame->width, i));
  135. if (ret < 0)
  136. return ret;
  137. if (!(frame->linesize[0] & (align-1)))
  138. break;
  139. }
  140. for (i = 0; i < 4 && frame->linesize[i]; i++)
  141. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  142. }
  143. for (i = 0; i < 4 && frame->linesize[i]; i++) {
  144. int h = FFALIGN(frame->height, 32);
  145. if (i == 1 || i == 2)
  146. h = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
  147. frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
  148. if (!frame->buf[i])
  149. goto fail;
  150. frame->data[i] = frame->buf[i]->data;
  151. }
  152. if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  153. av_buffer_unref(&frame->buf[1]);
  154. frame->buf[1] = av_buffer_alloc(1024);
  155. if (!frame->buf[1])
  156. goto fail;
  157. frame->data[1] = frame->buf[1]->data;
  158. }
  159. frame->extended_data = frame->data;
  160. return 0;
  161. fail:
  162. av_frame_unref(frame);
  163. return AVERROR(ENOMEM);
  164. }
  165. static int get_audio_buffer(AVFrame *frame, int align)
  166. {
  167. int channels;
  168. int planar = av_sample_fmt_is_planar(frame->format);
  169. int planes;
  170. int ret, i;
  171. if (!frame->channels)
  172. frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  173. channels = frame->channels;
  174. planes = planar ? channels : 1;
  175. CHECK_CHANNELS_CONSISTENCY(frame);
  176. if (!frame->linesize[0]) {
  177. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  178. frame->nb_samples, frame->format,
  179. align);
  180. if (ret < 0)
  181. return ret;
  182. }
  183. if (planes > AV_NUM_DATA_POINTERS) {
  184. frame->extended_data = av_mallocz_array(planes,
  185. sizeof(*frame->extended_data));
  186. frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
  187. sizeof(*frame->extended_buf));
  188. if (!frame->extended_data || !frame->extended_buf) {
  189. av_freep(&frame->extended_data);
  190. av_freep(&frame->extended_buf);
  191. return AVERROR(ENOMEM);
  192. }
  193. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  194. } else
  195. frame->extended_data = frame->data;
  196. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  197. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  198. if (!frame->buf[i]) {
  199. av_frame_unref(frame);
  200. return AVERROR(ENOMEM);
  201. }
  202. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  203. }
  204. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  205. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  206. if (!frame->extended_buf[i]) {
  207. av_frame_unref(frame);
  208. return AVERROR(ENOMEM);
  209. }
  210. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  211. }
  212. return 0;
  213. }
  214. int av_frame_get_buffer(AVFrame *frame, int align)
  215. {
  216. if (frame->format < 0)
  217. return AVERROR(EINVAL);
  218. if (frame->width > 0 && frame->height > 0)
  219. return get_video_buffer(frame, align);
  220. else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
  221. return get_audio_buffer(frame, align);
  222. return AVERROR(EINVAL);
  223. }
  224. int av_frame_ref(AVFrame *dst, const AVFrame *src)
  225. {
  226. int i, ret = 0;
  227. dst->format = src->format;
  228. dst->width = src->width;
  229. dst->height = src->height;
  230. dst->channels = src->channels;
  231. dst->channel_layout = src->channel_layout;
  232. dst->nb_samples = src->nb_samples;
  233. ret = av_frame_copy_props(dst, src);
  234. if (ret < 0)
  235. return ret;
  236. /* duplicate the frame data if it's not refcounted */
  237. if (!src->buf[0]) {
  238. ret = av_frame_get_buffer(dst, 32);
  239. if (ret < 0)
  240. return ret;
  241. ret = av_frame_copy(dst, src);
  242. if (ret < 0)
  243. av_frame_unref(dst);
  244. return ret;
  245. }
  246. /* ref the buffers */
  247. for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
  248. if (!src->buf[i])
  249. continue;
  250. dst->buf[i] = av_buffer_ref(src->buf[i]);
  251. if (!dst->buf[i]) {
  252. ret = AVERROR(ENOMEM);
  253. goto fail;
  254. }
  255. }
  256. if (src->extended_buf) {
  257. dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
  258. src->nb_extended_buf);
  259. if (!dst->extended_buf) {
  260. ret = AVERROR(ENOMEM);
  261. goto fail;
  262. }
  263. dst->nb_extended_buf = src->nb_extended_buf;
  264. for (i = 0; i < src->nb_extended_buf; i++) {
  265. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  266. if (!dst->extended_buf[i]) {
  267. ret = AVERROR(ENOMEM);
  268. goto fail;
  269. }
  270. }
  271. }
  272. /* duplicate extended data */
  273. if (src->extended_data != src->data) {
  274. int ch = src->channels;
  275. if (!ch) {
  276. ret = AVERROR(EINVAL);
  277. goto fail;
  278. }
  279. CHECK_CHANNELS_CONSISTENCY(src);
  280. dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
  281. if (!dst->extended_data) {
  282. ret = AVERROR(ENOMEM);
  283. goto fail;
  284. }
  285. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  286. } else
  287. dst->extended_data = dst->data;
  288. memcpy(dst->data, src->data, sizeof(src->data));
  289. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  290. return 0;
  291. fail:
  292. av_frame_unref(dst);
  293. return ret;
  294. }
  295. AVFrame *av_frame_clone(const AVFrame *src)
  296. {
  297. AVFrame *ret = av_frame_alloc();
  298. if (!ret)
  299. return NULL;
  300. if (av_frame_ref(ret, src) < 0)
  301. av_frame_free(&ret);
  302. return ret;
  303. }
  304. void av_frame_unref(AVFrame *frame)
  305. {
  306. int i;
  307. for (i = 0; i < frame->nb_side_data; i++) {
  308. free_side_data(&frame->side_data[i]);
  309. }
  310. av_freep(&frame->side_data);
  311. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  312. av_buffer_unref(&frame->buf[i]);
  313. for (i = 0; i < frame->nb_extended_buf; i++)
  314. av_buffer_unref(&frame->extended_buf[i]);
  315. av_freep(&frame->extended_buf);
  316. av_dict_free(&frame->metadata);
  317. av_buffer_unref(&frame->qp_table_buf);
  318. get_frame_defaults(frame);
  319. }
  320. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  321. {
  322. *dst = *src;
  323. if (src->extended_data == src->data)
  324. dst->extended_data = dst->data;
  325. memset(src, 0, sizeof(*src));
  326. get_frame_defaults(src);
  327. }
  328. int av_frame_is_writable(AVFrame *frame)
  329. {
  330. int i, ret = 1;
  331. /* assume non-refcounted frames are not writable */
  332. if (!frame->buf[0])
  333. return 0;
  334. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  335. if (frame->buf[i])
  336. ret &= !!av_buffer_is_writable(frame->buf[i]);
  337. for (i = 0; i < frame->nb_extended_buf; i++)
  338. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  339. return ret;
  340. }
  341. int av_frame_make_writable(AVFrame *frame)
  342. {
  343. AVFrame tmp;
  344. int ret;
  345. if (!frame->buf[0])
  346. return AVERROR(EINVAL);
  347. if (av_frame_is_writable(frame))
  348. return 0;
  349. memset(&tmp, 0, sizeof(tmp));
  350. tmp.format = frame->format;
  351. tmp.width = frame->width;
  352. tmp.height = frame->height;
  353. tmp.channels = frame->channels;
  354. tmp.channel_layout = frame->channel_layout;
  355. tmp.nb_samples = frame->nb_samples;
  356. ret = av_frame_get_buffer(&tmp, 32);
  357. if (ret < 0)
  358. return ret;
  359. ret = av_frame_copy(&tmp, frame);
  360. if (ret < 0) {
  361. av_frame_unref(&tmp);
  362. return ret;
  363. }
  364. ret = av_frame_copy_props(&tmp, frame);
  365. if (ret < 0) {
  366. av_frame_unref(&tmp);
  367. return ret;
  368. }
  369. av_frame_unref(frame);
  370. *frame = tmp;
  371. if (tmp.data == tmp.extended_data)
  372. frame->extended_data = frame->data;
  373. return 0;
  374. }
  375. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  376. {
  377. int i;
  378. dst->key_frame = src->key_frame;
  379. dst->pict_type = src->pict_type;
  380. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  381. dst->pts = src->pts;
  382. dst->repeat_pict = src->repeat_pict;
  383. dst->interlaced_frame = src->interlaced_frame;
  384. dst->top_field_first = src->top_field_first;
  385. dst->palette_has_changed = src->palette_has_changed;
  386. dst->sample_rate = src->sample_rate;
  387. dst->opaque = src->opaque;
  388. #if FF_API_AVFRAME_LAVC
  389. dst->type = src->type;
  390. #endif
  391. dst->pkt_pts = src->pkt_pts;
  392. dst->pkt_dts = src->pkt_dts;
  393. dst->pkt_pos = src->pkt_pos;
  394. dst->pkt_size = src->pkt_size;
  395. dst->pkt_duration = src->pkt_duration;
  396. dst->reordered_opaque = src->reordered_opaque;
  397. dst->quality = src->quality;
  398. dst->best_effort_timestamp = src->best_effort_timestamp;
  399. dst->coded_picture_number = src->coded_picture_number;
  400. dst->display_picture_number = src->display_picture_number;
  401. dst->flags = src->flags;
  402. dst->decode_error_flags = src->decode_error_flags;
  403. #if FF_API_AVFRAME_COLORSPACE
  404. dst->color_primaries = src->color_primaries;
  405. dst->color_trc = src->color_trc;
  406. dst->colorspace = src->colorspace;
  407. dst->color_range = src->color_range;
  408. dst->chroma_location = src->chroma_location;
  409. #endif
  410. av_dict_copy(&dst->metadata, src->metadata, 0);
  411. memcpy(dst->error, src->error, sizeof(dst->error));
  412. for (i = 0; i < src->nb_side_data; i++) {
  413. const AVFrameSideData *sd_src = src->side_data[i];
  414. AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
  415. sd_src->size);
  416. if (!sd_dst) {
  417. for (i = 0; i < dst->nb_side_data; i++) {
  418. free_side_data(&dst->side_data[i]);
  419. }
  420. av_freep(&dst->side_data);
  421. dst->nb_side_data = 0;
  422. return AVERROR(ENOMEM);
  423. }
  424. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  425. av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
  426. }
  427. dst->qscale_table = NULL;
  428. dst->qstride = 0;
  429. dst->qscale_type = 0;
  430. if (src->qp_table_buf) {
  431. dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
  432. if (dst->qp_table_buf) {
  433. dst->qscale_table = dst->qp_table_buf->data;
  434. dst->qstride = src->qstride;
  435. dst->qscale_type = src->qscale_type;
  436. }
  437. }
  438. return 0;
  439. }
  440. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  441. {
  442. uint8_t *data;
  443. int planes, i;
  444. if (frame->nb_samples) {
  445. int channels = frame->channels;
  446. if (!channels)
  447. return NULL;
  448. CHECK_CHANNELS_CONSISTENCY(frame);
  449. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  450. } else
  451. planes = 4;
  452. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  453. return NULL;
  454. data = frame->extended_data[plane];
  455. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  456. AVBufferRef *buf = frame->buf[i];
  457. if (data >= buf->data && data < buf->data + buf->size)
  458. return buf;
  459. }
  460. for (i = 0; i < frame->nb_extended_buf; i++) {
  461. AVBufferRef *buf = frame->extended_buf[i];
  462. if (data >= buf->data && data < buf->data + buf->size)
  463. return buf;
  464. }
  465. return NULL;
  466. }
  467. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  468. enum AVFrameSideDataType type,
  469. int size)
  470. {
  471. AVFrameSideData *ret, **tmp;
  472. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  473. return NULL;
  474. tmp = av_realloc(frame->side_data,
  475. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  476. if (!tmp)
  477. return NULL;
  478. frame->side_data = tmp;
  479. ret = av_mallocz(sizeof(*ret));
  480. if (!ret)
  481. return NULL;
  482. ret->data = av_malloc(size);
  483. if (!ret->data) {
  484. av_freep(&ret);
  485. return NULL;
  486. }
  487. ret->size = size;
  488. ret->type = type;
  489. frame->side_data[frame->nb_side_data++] = ret;
  490. return ret;
  491. }
  492. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  493. enum AVFrameSideDataType type)
  494. {
  495. int i;
  496. for (i = 0; i < frame->nb_side_data; i++) {
  497. if (frame->side_data[i]->type == type)
  498. return frame->side_data[i];
  499. }
  500. return NULL;
  501. }
  502. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  503. {
  504. const uint8_t *src_data[4];
  505. int i, planes;
  506. if (dst->width < src->width ||
  507. dst->height < src->height)
  508. return AVERROR(EINVAL);
  509. planes = av_pix_fmt_count_planes(dst->format);
  510. for (i = 0; i < planes; i++)
  511. if (!dst->data[i] || !src->data[i])
  512. return AVERROR(EINVAL);
  513. memcpy(src_data, src->data, sizeof(src_data));
  514. av_image_copy(dst->data, dst->linesize,
  515. src_data, src->linesize,
  516. dst->format, src->width, src->height);
  517. return 0;
  518. }
  519. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  520. {
  521. int planar = av_sample_fmt_is_planar(dst->format);
  522. int channels = dst->channels;
  523. int planes = planar ? channels : 1;
  524. int i;
  525. if (dst->nb_samples != src->nb_samples ||
  526. dst->channels != src->channels ||
  527. dst->channel_layout != src->channel_layout)
  528. return AVERROR(EINVAL);
  529. CHECK_CHANNELS_CONSISTENCY(src);
  530. for (i = 0; i < planes; i++)
  531. if (!dst->extended_data[i] || !src->extended_data[i])
  532. return AVERROR(EINVAL);
  533. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  534. dst->nb_samples, channels, dst->format);
  535. return 0;
  536. }
  537. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  538. {
  539. if (dst->format != src->format || dst->format < 0)
  540. return AVERROR(EINVAL);
  541. if (dst->width > 0 && dst->height > 0)
  542. return frame_copy_video(dst, src);
  543. else if (dst->nb_samples > 0 && dst->channel_layout)
  544. return frame_copy_audio(dst, src);
  545. return AVERROR(EINVAL);
  546. }
  547. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  548. {
  549. int i;
  550. for (i = 0; i < frame->nb_side_data; i++) {
  551. AVFrameSideData *sd = frame->side_data[i];
  552. if (sd->type == type) {
  553. free_side_data(&frame->side_data[i]);
  554. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  555. frame->nb_side_data--;
  556. }
  557. }
  558. }