frame.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 * const 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. frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
  93. frame->color_trc = AVCOL_TRC_UNSPECIFIED;
  94. frame->colorspace = AVCOL_SPC_UNSPECIFIED;
  95. frame->color_range = AVCOL_RANGE_UNSPECIFIED;
  96. frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  97. }
  98. static void free_side_data(AVFrameSideData **ptr_sd)
  99. {
  100. AVFrameSideData *sd = *ptr_sd;
  101. av_freep(&sd->data);
  102. av_dict_free(&sd->metadata);
  103. av_freep(ptr_sd);
  104. }
  105. AVFrame *av_frame_alloc(void)
  106. {
  107. AVFrame *frame = av_mallocz(sizeof(*frame));
  108. if (!frame)
  109. return NULL;
  110. frame->extended_data = NULL;
  111. get_frame_defaults(frame);
  112. return frame;
  113. }
  114. void av_frame_free(AVFrame **frame)
  115. {
  116. if (!frame || !*frame)
  117. return;
  118. av_frame_unref(*frame);
  119. av_freep(frame);
  120. }
  121. static int get_video_buffer(AVFrame *frame, int align)
  122. {
  123. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  124. int ret, i;
  125. if (!desc)
  126. return AVERROR(EINVAL);
  127. if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
  128. return ret;
  129. if (!frame->linesize[0]) {
  130. for(i=1; i<=align; i+=i) {
  131. ret = av_image_fill_linesizes(frame->linesize, frame->format,
  132. FFALIGN(frame->width, i));
  133. if (ret < 0)
  134. return ret;
  135. if (!(frame->linesize[0] & (align-1)))
  136. break;
  137. }
  138. for (i = 0; i < 4 && frame->linesize[i]; i++)
  139. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  140. }
  141. for (i = 0; i < 4 && frame->linesize[i]; i++) {
  142. int h = FFALIGN(frame->height, 32);
  143. if (i == 1 || i == 2)
  144. h = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
  145. frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
  146. if (!frame->buf[i])
  147. goto fail;
  148. frame->data[i] = frame->buf[i]->data;
  149. }
  150. if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  151. av_buffer_unref(&frame->buf[1]);
  152. frame->buf[1] = av_buffer_alloc(1024);
  153. if (!frame->buf[1])
  154. goto fail;
  155. frame->data[1] = frame->buf[1]->data;
  156. }
  157. frame->extended_data = frame->data;
  158. return 0;
  159. fail:
  160. av_frame_unref(frame);
  161. return AVERROR(ENOMEM);
  162. }
  163. static int get_audio_buffer(AVFrame *frame, int align)
  164. {
  165. int channels;
  166. int planar = av_sample_fmt_is_planar(frame->format);
  167. int planes;
  168. int ret, i;
  169. if (!frame->channels)
  170. frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  171. channels = frame->channels;
  172. planes = planar ? channels : 1;
  173. CHECK_CHANNELS_CONSISTENCY(frame);
  174. if (!frame->linesize[0]) {
  175. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  176. frame->nb_samples, frame->format,
  177. align);
  178. if (ret < 0)
  179. return ret;
  180. }
  181. if (planes > AV_NUM_DATA_POINTERS) {
  182. frame->extended_data = av_mallocz_array(planes,
  183. sizeof(*frame->extended_data));
  184. frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
  185. sizeof(*frame->extended_buf));
  186. if (!frame->extended_data || !frame->extended_buf) {
  187. av_freep(&frame->extended_data);
  188. av_freep(&frame->extended_buf);
  189. return AVERROR(ENOMEM);
  190. }
  191. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  192. } else
  193. frame->extended_data = frame->data;
  194. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  195. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  196. if (!frame->buf[i]) {
  197. av_frame_unref(frame);
  198. return AVERROR(ENOMEM);
  199. }
  200. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  201. }
  202. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  203. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  204. if (!frame->extended_buf[i]) {
  205. av_frame_unref(frame);
  206. return AVERROR(ENOMEM);
  207. }
  208. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  209. }
  210. return 0;
  211. }
  212. int av_frame_get_buffer(AVFrame *frame, int align)
  213. {
  214. if (frame->format < 0)
  215. return AVERROR(EINVAL);
  216. if (frame->width > 0 && frame->height > 0)
  217. return get_video_buffer(frame, align);
  218. else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
  219. return get_audio_buffer(frame, align);
  220. return AVERROR(EINVAL);
  221. }
  222. int av_frame_ref(AVFrame *dst, const AVFrame *src)
  223. {
  224. int i, ret = 0;
  225. dst->format = src->format;
  226. dst->width = src->width;
  227. dst->height = src->height;
  228. dst->channels = src->channels;
  229. dst->channel_layout = src->channel_layout;
  230. dst->nb_samples = src->nb_samples;
  231. ret = av_frame_copy_props(dst, src);
  232. if (ret < 0)
  233. return ret;
  234. /* duplicate the frame data if it's not refcounted */
  235. if (!src->buf[0]) {
  236. ret = av_frame_get_buffer(dst, 32);
  237. if (ret < 0)
  238. return ret;
  239. ret = av_frame_copy(dst, src);
  240. if (ret < 0)
  241. av_frame_unref(dst);
  242. return ret;
  243. }
  244. /* ref the buffers */
  245. for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
  246. if (!src->buf[i])
  247. continue;
  248. dst->buf[i] = av_buffer_ref(src->buf[i]);
  249. if (!dst->buf[i]) {
  250. ret = AVERROR(ENOMEM);
  251. goto fail;
  252. }
  253. }
  254. if (src->extended_buf) {
  255. dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
  256. src->nb_extended_buf);
  257. if (!dst->extended_buf) {
  258. ret = AVERROR(ENOMEM);
  259. goto fail;
  260. }
  261. dst->nb_extended_buf = src->nb_extended_buf;
  262. for (i = 0; i < src->nb_extended_buf; i++) {
  263. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  264. if (!dst->extended_buf[i]) {
  265. ret = AVERROR(ENOMEM);
  266. goto fail;
  267. }
  268. }
  269. }
  270. /* duplicate extended data */
  271. if (src->extended_data != src->data) {
  272. int ch = src->channels;
  273. if (!ch) {
  274. ret = AVERROR(EINVAL);
  275. goto fail;
  276. }
  277. CHECK_CHANNELS_CONSISTENCY(src);
  278. dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
  279. if (!dst->extended_data) {
  280. ret = AVERROR(ENOMEM);
  281. goto fail;
  282. }
  283. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  284. } else
  285. dst->extended_data = dst->data;
  286. memcpy(dst->data, src->data, sizeof(src->data));
  287. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  288. return 0;
  289. fail:
  290. av_frame_unref(dst);
  291. return ret;
  292. }
  293. AVFrame *av_frame_clone(const AVFrame *src)
  294. {
  295. AVFrame *ret = av_frame_alloc();
  296. if (!ret)
  297. return NULL;
  298. if (av_frame_ref(ret, src) < 0)
  299. av_frame_free(&ret);
  300. return ret;
  301. }
  302. void av_frame_unref(AVFrame *frame)
  303. {
  304. int i;
  305. for (i = 0; i < frame->nb_side_data; i++) {
  306. free_side_data(&frame->side_data[i]);
  307. }
  308. av_freep(&frame->side_data);
  309. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  310. av_buffer_unref(&frame->buf[i]);
  311. for (i = 0; i < frame->nb_extended_buf; i++)
  312. av_buffer_unref(&frame->extended_buf[i]);
  313. av_freep(&frame->extended_buf);
  314. av_dict_free(&frame->metadata);
  315. av_buffer_unref(&frame->qp_table_buf);
  316. get_frame_defaults(frame);
  317. }
  318. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  319. {
  320. *dst = *src;
  321. if (src->extended_data == src->data)
  322. dst->extended_data = dst->data;
  323. memset(src, 0, sizeof(*src));
  324. get_frame_defaults(src);
  325. }
  326. int av_frame_is_writable(AVFrame *frame)
  327. {
  328. int i, ret = 1;
  329. /* assume non-refcounted frames are not writable */
  330. if (!frame->buf[0])
  331. return 0;
  332. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  333. if (frame->buf[i])
  334. ret &= !!av_buffer_is_writable(frame->buf[i]);
  335. for (i = 0; i < frame->nb_extended_buf; i++)
  336. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  337. return ret;
  338. }
  339. int av_frame_make_writable(AVFrame *frame)
  340. {
  341. AVFrame tmp;
  342. int ret;
  343. if (!frame->buf[0])
  344. return AVERROR(EINVAL);
  345. if (av_frame_is_writable(frame))
  346. return 0;
  347. memset(&tmp, 0, sizeof(tmp));
  348. tmp.format = frame->format;
  349. tmp.width = frame->width;
  350. tmp.height = frame->height;
  351. tmp.channels = frame->channels;
  352. tmp.channel_layout = frame->channel_layout;
  353. tmp.nb_samples = frame->nb_samples;
  354. ret = av_frame_get_buffer(&tmp, 32);
  355. if (ret < 0)
  356. return ret;
  357. ret = av_frame_copy(&tmp, frame);
  358. if (ret < 0) {
  359. av_frame_unref(&tmp);
  360. return ret;
  361. }
  362. ret = av_frame_copy_props(&tmp, frame);
  363. if (ret < 0) {
  364. av_frame_unref(&tmp);
  365. return ret;
  366. }
  367. av_frame_unref(frame);
  368. *frame = tmp;
  369. if (tmp.data == tmp.extended_data)
  370. frame->extended_data = frame->data;
  371. return 0;
  372. }
  373. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  374. {
  375. int i;
  376. dst->key_frame = src->key_frame;
  377. dst->pict_type = src->pict_type;
  378. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  379. dst->pts = src->pts;
  380. dst->repeat_pict = src->repeat_pict;
  381. dst->interlaced_frame = src->interlaced_frame;
  382. dst->top_field_first = src->top_field_first;
  383. dst->palette_has_changed = src->palette_has_changed;
  384. dst->sample_rate = src->sample_rate;
  385. dst->opaque = src->opaque;
  386. #if FF_API_AVFRAME_LAVC
  387. dst->type = src->type;
  388. #endif
  389. dst->pkt_pts = src->pkt_pts;
  390. dst->pkt_dts = src->pkt_dts;
  391. dst->pkt_pos = src->pkt_pos;
  392. dst->pkt_size = src->pkt_size;
  393. dst->pkt_duration = src->pkt_duration;
  394. dst->reordered_opaque = src->reordered_opaque;
  395. dst->quality = src->quality;
  396. dst->best_effort_timestamp = src->best_effort_timestamp;
  397. dst->coded_picture_number = src->coded_picture_number;
  398. dst->display_picture_number = src->display_picture_number;
  399. dst->flags = src->flags;
  400. dst->decode_error_flags = src->decode_error_flags;
  401. dst->color_primaries = src->color_primaries;
  402. dst->color_trc = src->color_trc;
  403. dst->colorspace = src->colorspace;
  404. dst->color_range = src->color_range;
  405. dst->chroma_location = src->chroma_location;
  406. av_dict_copy(&dst->metadata, src->metadata, 0);
  407. memcpy(dst->error, src->error, sizeof(dst->error));
  408. for (i = 0; i < src->nb_side_data; i++) {
  409. const AVFrameSideData *sd_src = src->side_data[i];
  410. AVFrameSideData *sd_dst;
  411. if ( sd_src->type == AV_FRAME_DATA_PANSCAN
  412. && (src->width != dst->width || src->height != dst->height))
  413. continue;
  414. 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. }
  559. const char *av_frame_side_data_name(enum AVFrameSideDataType type)
  560. {
  561. switch(type) {
  562. case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
  563. case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
  564. case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
  565. case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
  566. case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
  567. case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
  568. case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
  569. case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
  570. }
  571. return NULL;
  572. }