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.

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