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.

780 lines
23KB

  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. static AVFrameSideData *frame_new_side_data(AVFrame *frame,
  28. enum AVFrameSideDataType type,
  29. AVBufferRef *buf);
  30. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  31. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  32. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  33. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  34. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  35. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  36. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  37. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  38. MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
  39. MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
  40. MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
  41. #define CHECK_CHANNELS_CONSISTENCY(frame) \
  42. av_assert2(!(frame)->channel_layout || \
  43. (frame)->channels == \
  44. av_get_channel_layout_nb_channels((frame)->channel_layout))
  45. AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
  46. #if FF_API_FRAME_QP
  47. int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
  48. {
  49. av_buffer_unref(&f->qp_table_buf);
  50. f->qp_table_buf = buf;
  51. FF_DISABLE_DEPRECATION_WARNINGS
  52. f->qscale_table = buf->data;
  53. f->qstride = stride;
  54. f->qscale_type = qp_type;
  55. FF_ENABLE_DEPRECATION_WARNINGS
  56. return 0;
  57. }
  58. int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
  59. {
  60. FF_DISABLE_DEPRECATION_WARNINGS
  61. *stride = f->qstride;
  62. *type = f->qscale_type;
  63. FF_ENABLE_DEPRECATION_WARNINGS
  64. if (!f->qp_table_buf)
  65. return NULL;
  66. return f->qp_table_buf->data;
  67. }
  68. #endif
  69. const char *av_get_colorspace_name(enum AVColorSpace val)
  70. {
  71. static const char * const name[] = {
  72. [AVCOL_SPC_RGB] = "GBR",
  73. [AVCOL_SPC_BT709] = "bt709",
  74. [AVCOL_SPC_FCC] = "fcc",
  75. [AVCOL_SPC_BT470BG] = "bt470bg",
  76. [AVCOL_SPC_SMPTE170M] = "smpte170m",
  77. [AVCOL_SPC_SMPTE240M] = "smpte240m",
  78. [AVCOL_SPC_YCOCG] = "YCgCo",
  79. };
  80. if ((unsigned)val >= FF_ARRAY_ELEMS(name))
  81. return NULL;
  82. return name[val];
  83. }
  84. static void get_frame_defaults(AVFrame *frame)
  85. {
  86. if (frame->extended_data != frame->data)
  87. av_freep(&frame->extended_data);
  88. memset(frame, 0, sizeof(*frame));
  89. frame->pts =
  90. frame->pkt_dts = AV_NOPTS_VALUE;
  91. #if FF_API_PKT_PTS
  92. FF_DISABLE_DEPRECATION_WARNINGS
  93. frame->pkt_pts = AV_NOPTS_VALUE;
  94. FF_ENABLE_DEPRECATION_WARNINGS
  95. #endif
  96. frame->best_effort_timestamp = AV_NOPTS_VALUE;
  97. frame->pkt_duration = 0;
  98. frame->pkt_pos = -1;
  99. frame->pkt_size = -1;
  100. frame->key_frame = 1;
  101. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  102. frame->format = -1; /* unknown */
  103. frame->extended_data = frame->data;
  104. frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
  105. frame->color_trc = AVCOL_TRC_UNSPECIFIED;
  106. frame->colorspace = AVCOL_SPC_UNSPECIFIED;
  107. frame->color_range = AVCOL_RANGE_UNSPECIFIED;
  108. frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  109. frame->flags = 0;
  110. }
  111. static void free_side_data(AVFrameSideData **ptr_sd)
  112. {
  113. AVFrameSideData *sd = *ptr_sd;
  114. av_buffer_unref(&sd->buf);
  115. av_dict_free(&sd->metadata);
  116. av_freep(ptr_sd);
  117. }
  118. static void wipe_side_data(AVFrame *frame)
  119. {
  120. int i;
  121. for (i = 0; i < frame->nb_side_data; i++) {
  122. free_side_data(&frame->side_data[i]);
  123. }
  124. frame->nb_side_data = 0;
  125. av_freep(&frame->side_data);
  126. }
  127. AVFrame *av_frame_alloc(void)
  128. {
  129. AVFrame *frame = av_mallocz(sizeof(*frame));
  130. if (!frame)
  131. return NULL;
  132. frame->extended_data = NULL;
  133. get_frame_defaults(frame);
  134. return frame;
  135. }
  136. void av_frame_free(AVFrame **frame)
  137. {
  138. if (!frame || !*frame)
  139. return;
  140. av_frame_unref(*frame);
  141. av_freep(frame);
  142. }
  143. static int get_video_buffer(AVFrame *frame, int align)
  144. {
  145. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  146. int ret, i;
  147. if (!desc)
  148. return AVERROR(EINVAL);
  149. if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
  150. return ret;
  151. if (!frame->linesize[0]) {
  152. for(i=1; i<=align; i+=i) {
  153. ret = av_image_fill_linesizes(frame->linesize, frame->format,
  154. FFALIGN(frame->width, i));
  155. if (ret < 0)
  156. return ret;
  157. if (!(frame->linesize[0] & (align-1)))
  158. break;
  159. }
  160. for (i = 0; i < 4 && frame->linesize[i]; i++)
  161. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  162. }
  163. for (i = 0; i < 4 && frame->linesize[i]; i++) {
  164. int h = FFALIGN(frame->height, 32);
  165. if (i == 1 || i == 2)
  166. h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
  167. frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
  168. if (!frame->buf[i])
  169. goto fail;
  170. frame->data[i] = frame->buf[i]->data;
  171. }
  172. if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  173. av_buffer_unref(&frame->buf[1]);
  174. frame->buf[1] = av_buffer_alloc(AVPALETTE_SIZE);
  175. if (!frame->buf[1])
  176. goto fail;
  177. frame->data[1] = frame->buf[1]->data;
  178. }
  179. frame->extended_data = frame->data;
  180. return 0;
  181. fail:
  182. av_frame_unref(frame);
  183. return AVERROR(ENOMEM);
  184. }
  185. static int get_audio_buffer(AVFrame *frame, int align)
  186. {
  187. int channels;
  188. int planar = av_sample_fmt_is_planar(frame->format);
  189. int planes;
  190. int ret, i;
  191. if (!frame->channels)
  192. frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  193. channels = frame->channels;
  194. planes = planar ? channels : 1;
  195. CHECK_CHANNELS_CONSISTENCY(frame);
  196. if (!frame->linesize[0]) {
  197. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  198. frame->nb_samples, frame->format,
  199. align);
  200. if (ret < 0)
  201. return ret;
  202. }
  203. if (planes > AV_NUM_DATA_POINTERS) {
  204. frame->extended_data = av_mallocz_array(planes,
  205. sizeof(*frame->extended_data));
  206. frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
  207. sizeof(*frame->extended_buf));
  208. if (!frame->extended_data || !frame->extended_buf) {
  209. av_freep(&frame->extended_data);
  210. av_freep(&frame->extended_buf);
  211. return AVERROR(ENOMEM);
  212. }
  213. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  214. } else
  215. frame->extended_data = frame->data;
  216. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  217. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  218. if (!frame->buf[i]) {
  219. av_frame_unref(frame);
  220. return AVERROR(ENOMEM);
  221. }
  222. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  223. }
  224. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  225. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  226. if (!frame->extended_buf[i]) {
  227. av_frame_unref(frame);
  228. return AVERROR(ENOMEM);
  229. }
  230. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  231. }
  232. return 0;
  233. }
  234. int av_frame_get_buffer(AVFrame *frame, int align)
  235. {
  236. if (frame->format < 0)
  237. return AVERROR(EINVAL);
  238. if (frame->width > 0 && frame->height > 0)
  239. return get_video_buffer(frame, align);
  240. else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
  241. return get_audio_buffer(frame, align);
  242. return AVERROR(EINVAL);
  243. }
  244. static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
  245. {
  246. int i;
  247. dst->key_frame = src->key_frame;
  248. dst->pict_type = src->pict_type;
  249. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  250. dst->pts = src->pts;
  251. dst->repeat_pict = src->repeat_pict;
  252. dst->interlaced_frame = src->interlaced_frame;
  253. dst->top_field_first = src->top_field_first;
  254. dst->palette_has_changed = src->palette_has_changed;
  255. dst->sample_rate = src->sample_rate;
  256. dst->opaque = src->opaque;
  257. #if FF_API_PKT_PTS
  258. FF_DISABLE_DEPRECATION_WARNINGS
  259. dst->pkt_pts = src->pkt_pts;
  260. FF_ENABLE_DEPRECATION_WARNINGS
  261. #endif
  262. dst->pkt_dts = src->pkt_dts;
  263. dst->pkt_pos = src->pkt_pos;
  264. dst->pkt_size = src->pkt_size;
  265. dst->pkt_duration = src->pkt_duration;
  266. dst->reordered_opaque = src->reordered_opaque;
  267. dst->quality = src->quality;
  268. dst->best_effort_timestamp = src->best_effort_timestamp;
  269. dst->coded_picture_number = src->coded_picture_number;
  270. dst->display_picture_number = src->display_picture_number;
  271. dst->flags = src->flags;
  272. dst->decode_error_flags = src->decode_error_flags;
  273. dst->color_primaries = src->color_primaries;
  274. dst->color_trc = src->color_trc;
  275. dst->colorspace = src->colorspace;
  276. dst->color_range = src->color_range;
  277. dst->chroma_location = src->chroma_location;
  278. av_dict_copy(&dst->metadata, src->metadata, 0);
  279. #if FF_API_ERROR_FRAME
  280. FF_DISABLE_DEPRECATION_WARNINGS
  281. memcpy(dst->error, src->error, sizeof(dst->error));
  282. FF_ENABLE_DEPRECATION_WARNINGS
  283. #endif
  284. for (i = 0; i < src->nb_side_data; i++) {
  285. const AVFrameSideData *sd_src = src->side_data[i];
  286. AVFrameSideData *sd_dst;
  287. if ( sd_src->type == AV_FRAME_DATA_PANSCAN
  288. && (src->width != dst->width || src->height != dst->height))
  289. continue;
  290. if (force_copy) {
  291. sd_dst = av_frame_new_side_data(dst, sd_src->type,
  292. sd_src->size);
  293. if (!sd_dst) {
  294. wipe_side_data(dst);
  295. return AVERROR(ENOMEM);
  296. }
  297. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  298. } else {
  299. sd_dst = frame_new_side_data(dst, sd_src->type, av_buffer_ref(sd_src->buf));
  300. if (!sd_dst) {
  301. wipe_side_data(dst);
  302. return AVERROR(ENOMEM);
  303. }
  304. }
  305. av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
  306. }
  307. #if FF_API_FRAME_QP
  308. FF_DISABLE_DEPRECATION_WARNINGS
  309. dst->qscale_table = NULL;
  310. dst->qstride = 0;
  311. dst->qscale_type = 0;
  312. av_buffer_unref(&dst->qp_table_buf);
  313. if (src->qp_table_buf) {
  314. dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
  315. if (dst->qp_table_buf) {
  316. dst->qscale_table = dst->qp_table_buf->data;
  317. dst->qstride = src->qstride;
  318. dst->qscale_type = src->qscale_type;
  319. }
  320. }
  321. FF_ENABLE_DEPRECATION_WARNINGS
  322. #endif
  323. av_buffer_unref(&dst->opaque_ref);
  324. if (src->opaque_ref) {
  325. dst->opaque_ref = av_buffer_ref(src->opaque_ref);
  326. if (!dst->opaque_ref)
  327. return AVERROR(ENOMEM);
  328. }
  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. av_buffer_unref(&frame->opaque_ref);
  437. get_frame_defaults(frame);
  438. }
  439. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  440. {
  441. av_assert1(dst->width == 0 && dst->height == 0);
  442. av_assert1(dst->channels == 0);
  443. *dst = *src;
  444. if (src->extended_data == src->data)
  445. dst->extended_data = dst->data;
  446. memset(src, 0, sizeof(*src));
  447. get_frame_defaults(src);
  448. }
  449. int av_frame_is_writable(AVFrame *frame)
  450. {
  451. int i, ret = 1;
  452. /* assume non-refcounted frames are not writable */
  453. if (!frame->buf[0])
  454. return 0;
  455. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  456. if (frame->buf[i])
  457. ret &= !!av_buffer_is_writable(frame->buf[i]);
  458. for (i = 0; i < frame->nb_extended_buf; i++)
  459. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  460. return ret;
  461. }
  462. int av_frame_make_writable(AVFrame *frame)
  463. {
  464. AVFrame tmp;
  465. int ret;
  466. if (!frame->buf[0])
  467. return AVERROR(EINVAL);
  468. if (av_frame_is_writable(frame))
  469. return 0;
  470. memset(&tmp, 0, sizeof(tmp));
  471. tmp.format = frame->format;
  472. tmp.width = frame->width;
  473. tmp.height = frame->height;
  474. tmp.channels = frame->channels;
  475. tmp.channel_layout = frame->channel_layout;
  476. tmp.nb_samples = frame->nb_samples;
  477. ret = av_frame_get_buffer(&tmp, 32);
  478. if (ret < 0)
  479. return ret;
  480. ret = av_frame_copy(&tmp, frame);
  481. if (ret < 0) {
  482. av_frame_unref(&tmp);
  483. return ret;
  484. }
  485. ret = av_frame_copy_props(&tmp, frame);
  486. if (ret < 0) {
  487. av_frame_unref(&tmp);
  488. return ret;
  489. }
  490. av_frame_unref(frame);
  491. *frame = tmp;
  492. if (tmp.data == tmp.extended_data)
  493. frame->extended_data = frame->data;
  494. return 0;
  495. }
  496. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  497. {
  498. return frame_copy_props(dst, src, 1);
  499. }
  500. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  501. {
  502. uint8_t *data;
  503. int planes, i;
  504. if (frame->nb_samples) {
  505. int channels = frame->channels;
  506. if (!channels)
  507. return NULL;
  508. CHECK_CHANNELS_CONSISTENCY(frame);
  509. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  510. } else
  511. planes = 4;
  512. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  513. return NULL;
  514. data = frame->extended_data[plane];
  515. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  516. AVBufferRef *buf = frame->buf[i];
  517. if (data >= buf->data && data < buf->data + buf->size)
  518. return buf;
  519. }
  520. for (i = 0; i < frame->nb_extended_buf; i++) {
  521. AVBufferRef *buf = frame->extended_buf[i];
  522. if (data >= buf->data && data < buf->data + buf->size)
  523. return buf;
  524. }
  525. return NULL;
  526. }
  527. static AVFrameSideData *frame_new_side_data(AVFrame *frame,
  528. enum AVFrameSideDataType type,
  529. AVBufferRef *buf)
  530. {
  531. AVFrameSideData *ret, **tmp;
  532. if (!buf)
  533. return NULL;
  534. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  535. goto fail;
  536. tmp = av_realloc(frame->side_data,
  537. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  538. if (!tmp)
  539. goto fail;
  540. frame->side_data = tmp;
  541. ret = av_mallocz(sizeof(*ret));
  542. if (!ret)
  543. goto fail;
  544. ret->buf = buf;
  545. ret->data = ret->buf->data;
  546. ret->size = buf->size;
  547. ret->type = type;
  548. frame->side_data[frame->nb_side_data++] = ret;
  549. return ret;
  550. fail:
  551. av_buffer_unref(&buf);
  552. return NULL;
  553. }
  554. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  555. enum AVFrameSideDataType type,
  556. int size)
  557. {
  558. return frame_new_side_data(frame, type, av_buffer_alloc(size));
  559. }
  560. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  561. enum AVFrameSideDataType type)
  562. {
  563. int i;
  564. for (i = 0; i < frame->nb_side_data; i++) {
  565. if (frame->side_data[i]->type == type)
  566. return frame->side_data[i];
  567. }
  568. return NULL;
  569. }
  570. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  571. {
  572. const uint8_t *src_data[4];
  573. int i, planes;
  574. if (dst->width < src->width ||
  575. dst->height < src->height)
  576. return AVERROR(EINVAL);
  577. planes = av_pix_fmt_count_planes(dst->format);
  578. for (i = 0; i < planes; i++)
  579. if (!dst->data[i] || !src->data[i])
  580. return AVERROR(EINVAL);
  581. memcpy(src_data, src->data, sizeof(src_data));
  582. av_image_copy(dst->data, dst->linesize,
  583. src_data, src->linesize,
  584. dst->format, src->width, src->height);
  585. return 0;
  586. }
  587. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  588. {
  589. int planar = av_sample_fmt_is_planar(dst->format);
  590. int channels = dst->channels;
  591. int planes = planar ? channels : 1;
  592. int i;
  593. if (dst->nb_samples != src->nb_samples ||
  594. dst->channels != src->channels ||
  595. dst->channel_layout != src->channel_layout)
  596. return AVERROR(EINVAL);
  597. CHECK_CHANNELS_CONSISTENCY(src);
  598. for (i = 0; i < planes; i++)
  599. if (!dst->extended_data[i] || !src->extended_data[i])
  600. return AVERROR(EINVAL);
  601. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  602. dst->nb_samples, channels, dst->format);
  603. return 0;
  604. }
  605. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  606. {
  607. if (dst->format != src->format || dst->format < 0)
  608. return AVERROR(EINVAL);
  609. if (dst->width > 0 && dst->height > 0)
  610. return frame_copy_video(dst, src);
  611. else if (dst->nb_samples > 0 && dst->channels > 0)
  612. return frame_copy_audio(dst, src);
  613. return AVERROR(EINVAL);
  614. }
  615. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  616. {
  617. int i;
  618. for (i = 0; i < frame->nb_side_data; i++) {
  619. AVFrameSideData *sd = frame->side_data[i];
  620. if (sd->type == type) {
  621. free_side_data(&frame->side_data[i]);
  622. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  623. frame->nb_side_data--;
  624. }
  625. }
  626. }
  627. const char *av_frame_side_data_name(enum AVFrameSideDataType type)
  628. {
  629. switch(type) {
  630. case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
  631. case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
  632. case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
  633. case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
  634. case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
  635. case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
  636. case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
  637. case AV_FRAME_DATA_AFD: return "Active format description";
  638. case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
  639. case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
  640. case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
  641. case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
  642. case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
  643. case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";
  644. }
  645. return NULL;
  646. }