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.

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