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.

894 lines
26KB

  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. #if FF_API_FRAME_GET_SET
  31. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  32. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  33. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  34. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  35. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  36. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  37. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  38. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  39. MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
  40. MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
  41. MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
  42. #endif
  43. #define CHECK_CHANNELS_CONSISTENCY(frame) \
  44. av_assert2(!(frame)->channel_layout || \
  45. (frame)->channels == \
  46. av_get_channel_layout_nb_channels((frame)->channel_layout))
  47. #if FF_API_FRAME_QP
  48. int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
  49. {
  50. av_buffer_unref(&f->qp_table_buf);
  51. f->qp_table_buf = buf;
  52. FF_DISABLE_DEPRECATION_WARNINGS
  53. f->qscale_table = buf->data;
  54. f->qstride = stride;
  55. f->qscale_type = qp_type;
  56. FF_ENABLE_DEPRECATION_WARNINGS
  57. return 0;
  58. }
  59. int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
  60. {
  61. FF_DISABLE_DEPRECATION_WARNINGS
  62. *stride = f->qstride;
  63. *type = f->qscale_type;
  64. FF_ENABLE_DEPRECATION_WARNINGS
  65. if (!f->qp_table_buf)
  66. return NULL;
  67. return f->qp_table_buf->data;
  68. }
  69. #endif
  70. const char *av_get_colorspace_name(enum AVColorSpace val)
  71. {
  72. static const char * const name[] = {
  73. [AVCOL_SPC_RGB] = "GBR",
  74. [AVCOL_SPC_BT709] = "bt709",
  75. [AVCOL_SPC_FCC] = "fcc",
  76. [AVCOL_SPC_BT470BG] = "bt470bg",
  77. [AVCOL_SPC_SMPTE170M] = "smpte170m",
  78. [AVCOL_SPC_SMPTE240M] = "smpte240m",
  79. [AVCOL_SPC_YCOCG] = "YCgCo",
  80. };
  81. if ((unsigned)val >= FF_ARRAY_ELEMS(name))
  82. return NULL;
  83. return name[val];
  84. }
  85. static void get_frame_defaults(AVFrame *frame)
  86. {
  87. if (frame->extended_data != frame->data)
  88. av_freep(&frame->extended_data);
  89. memset(frame, 0, sizeof(*frame));
  90. frame->pts =
  91. frame->pkt_dts = AV_NOPTS_VALUE;
  92. #if FF_API_PKT_PTS
  93. FF_DISABLE_DEPRECATION_WARNINGS
  94. frame->pkt_pts = AV_NOPTS_VALUE;
  95. FF_ENABLE_DEPRECATION_WARNINGS
  96. #endif
  97. frame->best_effort_timestamp = AV_NOPTS_VALUE;
  98. frame->pkt_duration = 0;
  99. frame->pkt_pos = -1;
  100. frame->pkt_size = -1;
  101. frame->key_frame = 1;
  102. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  103. frame->format = -1; /* unknown */
  104. frame->extended_data = frame->data;
  105. frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
  106. frame->color_trc = AVCOL_TRC_UNSPECIFIED;
  107. frame->colorspace = AVCOL_SPC_UNSPECIFIED;
  108. frame->color_range = AVCOL_RANGE_UNSPECIFIED;
  109. frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  110. frame->flags = 0;
  111. }
  112. static void free_side_data(AVFrameSideData **ptr_sd)
  113. {
  114. AVFrameSideData *sd = *ptr_sd;
  115. av_buffer_unref(&sd->buf);
  116. av_dict_free(&sd->metadata);
  117. av_freep(ptr_sd);
  118. }
  119. static void wipe_side_data(AVFrame *frame)
  120. {
  121. int i;
  122. for (i = 0; i < frame->nb_side_data; i++) {
  123. free_side_data(&frame->side_data[i]);
  124. }
  125. frame->nb_side_data = 0;
  126. av_freep(&frame->side_data);
  127. }
  128. AVFrame *av_frame_alloc(void)
  129. {
  130. AVFrame *frame = av_mallocz(sizeof(*frame));
  131. if (!frame)
  132. return NULL;
  133. frame->extended_data = NULL;
  134. get_frame_defaults(frame);
  135. return frame;
  136. }
  137. void av_frame_free(AVFrame **frame)
  138. {
  139. if (!frame || !*frame)
  140. return;
  141. av_frame_unref(*frame);
  142. av_freep(frame);
  143. }
  144. static int get_video_buffer(AVFrame *frame, int align)
  145. {
  146. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  147. int ret, i;
  148. if (!desc)
  149. return AVERROR(EINVAL);
  150. if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
  151. return ret;
  152. if (!frame->linesize[0]) {
  153. if (align <= 0)
  154. align = 32; /* STRIDE_ALIGN. Should be av_cpu_max_align() */
  155. for(i=1; i<=align; i+=i) {
  156. ret = av_image_fill_linesizes(frame->linesize, frame->format,
  157. FFALIGN(frame->width, i));
  158. if (ret < 0)
  159. return ret;
  160. if (!(frame->linesize[0] & (align-1)))
  161. break;
  162. }
  163. for (i = 0; i < 4 && frame->linesize[i]; i++)
  164. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  165. }
  166. for (i = 0; i < 4 && frame->linesize[i]; i++) {
  167. int h = FFALIGN(frame->height, 32);
  168. if (i == 1 || i == 2)
  169. h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
  170. frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
  171. if (!frame->buf[i])
  172. goto fail;
  173. frame->data[i] = frame->buf[i]->data;
  174. }
  175. if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  176. av_buffer_unref(&frame->buf[1]);
  177. frame->buf[1] = av_buffer_alloc(AVPALETTE_SIZE);
  178. if (!frame->buf[1])
  179. goto fail;
  180. frame->data[1] = frame->buf[1]->data;
  181. }
  182. frame->extended_data = frame->data;
  183. return 0;
  184. fail:
  185. av_frame_unref(frame);
  186. return AVERROR(ENOMEM);
  187. }
  188. static int get_audio_buffer(AVFrame *frame, int align)
  189. {
  190. int channels;
  191. int planar = av_sample_fmt_is_planar(frame->format);
  192. int planes;
  193. int ret, i;
  194. if (!frame->channels)
  195. frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  196. channels = frame->channels;
  197. planes = planar ? channels : 1;
  198. CHECK_CHANNELS_CONSISTENCY(frame);
  199. if (!frame->linesize[0]) {
  200. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  201. frame->nb_samples, frame->format,
  202. align);
  203. if (ret < 0)
  204. return ret;
  205. }
  206. if (planes > AV_NUM_DATA_POINTERS) {
  207. frame->extended_data = av_mallocz_array(planes,
  208. sizeof(*frame->extended_data));
  209. frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
  210. sizeof(*frame->extended_buf));
  211. if (!frame->extended_data || !frame->extended_buf) {
  212. av_freep(&frame->extended_data);
  213. av_freep(&frame->extended_buf);
  214. return AVERROR(ENOMEM);
  215. }
  216. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  217. } else
  218. frame->extended_data = frame->data;
  219. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  220. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  221. if (!frame->buf[i]) {
  222. av_frame_unref(frame);
  223. return AVERROR(ENOMEM);
  224. }
  225. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  226. }
  227. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  228. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  229. if (!frame->extended_buf[i]) {
  230. av_frame_unref(frame);
  231. return AVERROR(ENOMEM);
  232. }
  233. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  234. }
  235. return 0;
  236. }
  237. int av_frame_get_buffer(AVFrame *frame, int align)
  238. {
  239. if (frame->format < 0)
  240. return AVERROR(EINVAL);
  241. if (frame->width > 0 && frame->height > 0)
  242. return get_video_buffer(frame, align);
  243. else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
  244. return get_audio_buffer(frame, align);
  245. return AVERROR(EINVAL);
  246. }
  247. static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
  248. {
  249. int i;
  250. dst->key_frame = src->key_frame;
  251. dst->pict_type = src->pict_type;
  252. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  253. dst->crop_top = src->crop_top;
  254. dst->crop_bottom = src->crop_bottom;
  255. dst->crop_left = src->crop_left;
  256. dst->crop_right = src->crop_right;
  257. dst->pts = src->pts;
  258. dst->repeat_pict = src->repeat_pict;
  259. dst->interlaced_frame = src->interlaced_frame;
  260. dst->top_field_first = src->top_field_first;
  261. dst->palette_has_changed = src->palette_has_changed;
  262. dst->sample_rate = src->sample_rate;
  263. dst->opaque = src->opaque;
  264. #if FF_API_PKT_PTS
  265. FF_DISABLE_DEPRECATION_WARNINGS
  266. dst->pkt_pts = src->pkt_pts;
  267. FF_ENABLE_DEPRECATION_WARNINGS
  268. #endif
  269. dst->pkt_dts = src->pkt_dts;
  270. dst->pkt_pos = src->pkt_pos;
  271. dst->pkt_size = src->pkt_size;
  272. dst->pkt_duration = src->pkt_duration;
  273. dst->reordered_opaque = src->reordered_opaque;
  274. dst->quality = src->quality;
  275. dst->best_effort_timestamp = src->best_effort_timestamp;
  276. dst->coded_picture_number = src->coded_picture_number;
  277. dst->display_picture_number = src->display_picture_number;
  278. dst->flags = src->flags;
  279. dst->decode_error_flags = src->decode_error_flags;
  280. dst->color_primaries = src->color_primaries;
  281. dst->color_trc = src->color_trc;
  282. dst->colorspace = src->colorspace;
  283. dst->color_range = src->color_range;
  284. dst->chroma_location = src->chroma_location;
  285. av_dict_copy(&dst->metadata, src->metadata, 0);
  286. #if FF_API_ERROR_FRAME
  287. FF_DISABLE_DEPRECATION_WARNINGS
  288. memcpy(dst->error, src->error, sizeof(dst->error));
  289. FF_ENABLE_DEPRECATION_WARNINGS
  290. #endif
  291. for (i = 0; i < src->nb_side_data; i++) {
  292. const AVFrameSideData *sd_src = src->side_data[i];
  293. AVFrameSideData *sd_dst;
  294. if ( sd_src->type == AV_FRAME_DATA_PANSCAN
  295. && (src->width != dst->width || src->height != dst->height))
  296. continue;
  297. if (force_copy) {
  298. sd_dst = av_frame_new_side_data(dst, sd_src->type,
  299. sd_src->size);
  300. if (!sd_dst) {
  301. wipe_side_data(dst);
  302. return AVERROR(ENOMEM);
  303. }
  304. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  305. } else {
  306. sd_dst = frame_new_side_data(dst, sd_src->type, av_buffer_ref(sd_src->buf));
  307. if (!sd_dst) {
  308. wipe_side_data(dst);
  309. return AVERROR(ENOMEM);
  310. }
  311. }
  312. av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
  313. }
  314. #if FF_API_FRAME_QP
  315. FF_DISABLE_DEPRECATION_WARNINGS
  316. dst->qscale_table = NULL;
  317. dst->qstride = 0;
  318. dst->qscale_type = 0;
  319. av_buffer_unref(&dst->qp_table_buf);
  320. if (src->qp_table_buf) {
  321. dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
  322. if (dst->qp_table_buf) {
  323. dst->qscale_table = dst->qp_table_buf->data;
  324. dst->qstride = src->qstride;
  325. dst->qscale_type = src->qscale_type;
  326. }
  327. }
  328. FF_ENABLE_DEPRECATION_WARNINGS
  329. #endif
  330. av_buffer_unref(&dst->opaque_ref);
  331. av_buffer_unref(&dst->private_ref);
  332. if (src->opaque_ref) {
  333. dst->opaque_ref = av_buffer_ref(src->opaque_ref);
  334. if (!dst->opaque_ref)
  335. return AVERROR(ENOMEM);
  336. }
  337. if (src->private_ref) {
  338. dst->private_ref = av_buffer_ref(src->private_ref);
  339. if (!dst->private_ref)
  340. return AVERROR(ENOMEM);
  341. }
  342. return 0;
  343. }
  344. int av_frame_ref(AVFrame *dst, const AVFrame *src)
  345. {
  346. int i, ret = 0;
  347. av_assert1(dst->width == 0 && dst->height == 0);
  348. av_assert1(dst->channels == 0);
  349. dst->format = src->format;
  350. dst->width = src->width;
  351. dst->height = src->height;
  352. dst->channels = src->channels;
  353. dst->channel_layout = src->channel_layout;
  354. dst->nb_samples = src->nb_samples;
  355. ret = frame_copy_props(dst, src, 0);
  356. if (ret < 0)
  357. return ret;
  358. /* duplicate the frame data if it's not refcounted */
  359. if (!src->buf[0]) {
  360. ret = av_frame_get_buffer(dst, 32);
  361. if (ret < 0)
  362. return ret;
  363. ret = av_frame_copy(dst, src);
  364. if (ret < 0)
  365. av_frame_unref(dst);
  366. return ret;
  367. }
  368. /* ref the buffers */
  369. for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
  370. if (!src->buf[i])
  371. continue;
  372. dst->buf[i] = av_buffer_ref(src->buf[i]);
  373. if (!dst->buf[i]) {
  374. ret = AVERROR(ENOMEM);
  375. goto fail;
  376. }
  377. }
  378. if (src->extended_buf) {
  379. dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
  380. src->nb_extended_buf);
  381. if (!dst->extended_buf) {
  382. ret = AVERROR(ENOMEM);
  383. goto fail;
  384. }
  385. dst->nb_extended_buf = src->nb_extended_buf;
  386. for (i = 0; i < src->nb_extended_buf; i++) {
  387. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  388. if (!dst->extended_buf[i]) {
  389. ret = AVERROR(ENOMEM);
  390. goto fail;
  391. }
  392. }
  393. }
  394. if (src->hw_frames_ctx) {
  395. dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
  396. if (!dst->hw_frames_ctx) {
  397. ret = AVERROR(ENOMEM);
  398. goto fail;
  399. }
  400. }
  401. /* duplicate extended data */
  402. if (src->extended_data != src->data) {
  403. int ch = src->channels;
  404. if (!ch) {
  405. ret = AVERROR(EINVAL);
  406. goto fail;
  407. }
  408. CHECK_CHANNELS_CONSISTENCY(src);
  409. dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
  410. if (!dst->extended_data) {
  411. ret = AVERROR(ENOMEM);
  412. goto fail;
  413. }
  414. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  415. } else
  416. dst->extended_data = dst->data;
  417. memcpy(dst->data, src->data, sizeof(src->data));
  418. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  419. return 0;
  420. fail:
  421. av_frame_unref(dst);
  422. return ret;
  423. }
  424. AVFrame *av_frame_clone(const AVFrame *src)
  425. {
  426. AVFrame *ret = av_frame_alloc();
  427. if (!ret)
  428. return NULL;
  429. if (av_frame_ref(ret, src) < 0)
  430. av_frame_free(&ret);
  431. return ret;
  432. }
  433. void av_frame_unref(AVFrame *frame)
  434. {
  435. int i;
  436. if (!frame)
  437. return;
  438. wipe_side_data(frame);
  439. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  440. av_buffer_unref(&frame->buf[i]);
  441. for (i = 0; i < frame->nb_extended_buf; i++)
  442. av_buffer_unref(&frame->extended_buf[i]);
  443. av_freep(&frame->extended_buf);
  444. av_dict_free(&frame->metadata);
  445. #if FF_API_FRAME_QP
  446. av_buffer_unref(&frame->qp_table_buf);
  447. #endif
  448. av_buffer_unref(&frame->hw_frames_ctx);
  449. av_buffer_unref(&frame->opaque_ref);
  450. av_buffer_unref(&frame->private_ref);
  451. get_frame_defaults(frame);
  452. }
  453. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  454. {
  455. av_assert1(dst->width == 0 && dst->height == 0);
  456. av_assert1(dst->channels == 0);
  457. *dst = *src;
  458. if (src->extended_data == src->data)
  459. dst->extended_data = dst->data;
  460. memset(src, 0, sizeof(*src));
  461. get_frame_defaults(src);
  462. }
  463. int av_frame_is_writable(AVFrame *frame)
  464. {
  465. int i, ret = 1;
  466. /* assume non-refcounted frames are not writable */
  467. if (!frame->buf[0])
  468. return 0;
  469. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  470. if (frame->buf[i])
  471. ret &= !!av_buffer_is_writable(frame->buf[i]);
  472. for (i = 0; i < frame->nb_extended_buf; i++)
  473. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  474. return ret;
  475. }
  476. int av_frame_make_writable(AVFrame *frame)
  477. {
  478. AVFrame tmp;
  479. int ret;
  480. if (!frame->buf[0])
  481. return AVERROR(EINVAL);
  482. if (av_frame_is_writable(frame))
  483. return 0;
  484. memset(&tmp, 0, sizeof(tmp));
  485. tmp.format = frame->format;
  486. tmp.width = frame->width;
  487. tmp.height = frame->height;
  488. tmp.channels = frame->channels;
  489. tmp.channel_layout = frame->channel_layout;
  490. tmp.nb_samples = frame->nb_samples;
  491. ret = av_frame_get_buffer(&tmp, 32);
  492. if (ret < 0)
  493. return ret;
  494. ret = av_frame_copy(&tmp, frame);
  495. if (ret < 0) {
  496. av_frame_unref(&tmp);
  497. return ret;
  498. }
  499. ret = av_frame_copy_props(&tmp, frame);
  500. if (ret < 0) {
  501. av_frame_unref(&tmp);
  502. return ret;
  503. }
  504. av_frame_unref(frame);
  505. *frame = tmp;
  506. if (tmp.data == tmp.extended_data)
  507. frame->extended_data = frame->data;
  508. return 0;
  509. }
  510. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  511. {
  512. return frame_copy_props(dst, src, 1);
  513. }
  514. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  515. {
  516. uint8_t *data;
  517. int planes, i;
  518. if (frame->nb_samples) {
  519. int channels = frame->channels;
  520. if (!channels)
  521. return NULL;
  522. CHECK_CHANNELS_CONSISTENCY(frame);
  523. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  524. } else
  525. planes = 4;
  526. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  527. return NULL;
  528. data = frame->extended_data[plane];
  529. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  530. AVBufferRef *buf = frame->buf[i];
  531. if (data >= buf->data && data < buf->data + buf->size)
  532. return buf;
  533. }
  534. for (i = 0; i < frame->nb_extended_buf; i++) {
  535. AVBufferRef *buf = frame->extended_buf[i];
  536. if (data >= buf->data && data < buf->data + buf->size)
  537. return buf;
  538. }
  539. return NULL;
  540. }
  541. static AVFrameSideData *frame_new_side_data(AVFrame *frame,
  542. enum AVFrameSideDataType type,
  543. AVBufferRef *buf)
  544. {
  545. AVFrameSideData *ret, **tmp;
  546. if (!buf)
  547. return NULL;
  548. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  549. goto fail;
  550. tmp = av_realloc(frame->side_data,
  551. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  552. if (!tmp)
  553. goto fail;
  554. frame->side_data = tmp;
  555. ret = av_mallocz(sizeof(*ret));
  556. if (!ret)
  557. goto fail;
  558. ret->buf = buf;
  559. ret->data = ret->buf->data;
  560. ret->size = buf->size;
  561. ret->type = type;
  562. frame->side_data[frame->nb_side_data++] = ret;
  563. return ret;
  564. fail:
  565. av_buffer_unref(&buf);
  566. return NULL;
  567. }
  568. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  569. enum AVFrameSideDataType type,
  570. int size)
  571. {
  572. return frame_new_side_data(frame, type, av_buffer_alloc(size));
  573. }
  574. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  575. enum AVFrameSideDataType type)
  576. {
  577. int i;
  578. for (i = 0; i < frame->nb_side_data; i++) {
  579. if (frame->side_data[i]->type == type)
  580. return frame->side_data[i];
  581. }
  582. return NULL;
  583. }
  584. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  585. {
  586. const uint8_t *src_data[4];
  587. int i, planes;
  588. if (dst->width < src->width ||
  589. dst->height < src->height)
  590. return AVERROR(EINVAL);
  591. planes = av_pix_fmt_count_planes(dst->format);
  592. for (i = 0; i < planes; i++)
  593. if (!dst->data[i] || !src->data[i])
  594. return AVERROR(EINVAL);
  595. memcpy(src_data, src->data, sizeof(src_data));
  596. av_image_copy(dst->data, dst->linesize,
  597. src_data, src->linesize,
  598. dst->format, src->width, src->height);
  599. return 0;
  600. }
  601. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  602. {
  603. int planar = av_sample_fmt_is_planar(dst->format);
  604. int channels = dst->channels;
  605. int planes = planar ? channels : 1;
  606. int i;
  607. if (dst->nb_samples != src->nb_samples ||
  608. dst->channels != src->channels ||
  609. dst->channel_layout != src->channel_layout)
  610. return AVERROR(EINVAL);
  611. CHECK_CHANNELS_CONSISTENCY(src);
  612. for (i = 0; i < planes; i++)
  613. if (!dst->extended_data[i] || !src->extended_data[i])
  614. return AVERROR(EINVAL);
  615. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  616. dst->nb_samples, channels, dst->format);
  617. return 0;
  618. }
  619. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  620. {
  621. if (dst->format != src->format || dst->format < 0)
  622. return AVERROR(EINVAL);
  623. if (dst->width > 0 && dst->height > 0)
  624. return frame_copy_video(dst, src);
  625. else if (dst->nb_samples > 0 && dst->channels > 0)
  626. return frame_copy_audio(dst, src);
  627. return AVERROR(EINVAL);
  628. }
  629. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  630. {
  631. int i;
  632. for (i = 0; i < frame->nb_side_data; i++) {
  633. AVFrameSideData *sd = frame->side_data[i];
  634. if (sd->type == type) {
  635. free_side_data(&frame->side_data[i]);
  636. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  637. frame->nb_side_data--;
  638. }
  639. }
  640. }
  641. const char *av_frame_side_data_name(enum AVFrameSideDataType type)
  642. {
  643. switch(type) {
  644. case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
  645. case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
  646. case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
  647. case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
  648. case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
  649. case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
  650. case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
  651. case AV_FRAME_DATA_AFD: return "Active format description";
  652. case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
  653. case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
  654. case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
  655. case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
  656. case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
  657. case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";
  658. case AV_FRAME_DATA_ICC_PROFILE: return "ICC profile";
  659. }
  660. return NULL;
  661. }
  662. static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
  663. const AVPixFmtDescriptor *desc)
  664. {
  665. int i, j;
  666. for (i = 0; frame->data[i]; i++) {
  667. const AVComponentDescriptor *comp = NULL;
  668. int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
  669. int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  670. if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL) && i == 1) {
  671. offsets[i] = 0;
  672. break;
  673. }
  674. /* find any component descriptor for this plane */
  675. for (j = 0; j < desc->nb_components; j++) {
  676. if (desc->comp[j].plane == i) {
  677. comp = &desc->comp[j];
  678. break;
  679. }
  680. }
  681. if (!comp)
  682. return AVERROR_BUG;
  683. offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] +
  684. (frame->crop_left >> shift_x) * comp->step;
  685. }
  686. return 0;
  687. }
  688. int av_frame_apply_cropping(AVFrame *frame, int flags)
  689. {
  690. const AVPixFmtDescriptor *desc;
  691. size_t offsets[4];
  692. int i;
  693. if (!(frame->width > 0 && frame->height > 0))
  694. return AVERROR(EINVAL);
  695. if (frame->crop_left >= INT_MAX - frame->crop_right ||
  696. frame->crop_top >= INT_MAX - frame->crop_bottom ||
  697. (frame->crop_left + frame->crop_right) >= frame->width ||
  698. (frame->crop_top + frame->crop_bottom) >= frame->height)
  699. return AVERROR(ERANGE);
  700. desc = av_pix_fmt_desc_get(frame->format);
  701. if (!desc)
  702. return AVERROR_BUG;
  703. /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
  704. * formats cannot be easily handled here either (and corresponding decoders
  705. * should not export any cropping anyway), so do the same for those as well.
  706. * */
  707. if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
  708. frame->width -= frame->crop_right;
  709. frame->height -= frame->crop_bottom;
  710. frame->crop_right = 0;
  711. frame->crop_bottom = 0;
  712. return 0;
  713. }
  714. /* calculate the offsets for each plane */
  715. calc_cropping_offsets(offsets, frame, desc);
  716. /* adjust the offsets to avoid breaking alignment */
  717. if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
  718. int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
  719. int min_log2_align = INT_MAX;
  720. for (i = 0; frame->data[i]; i++) {
  721. int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
  722. min_log2_align = FFMIN(log2_align, min_log2_align);
  723. }
  724. /* we assume, and it should always be true, that the data alignment is
  725. * related to the cropping alignment by a constant power-of-2 factor */
  726. if (log2_crop_align < min_log2_align)
  727. return AVERROR_BUG;
  728. if (min_log2_align < 5) {
  729. frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
  730. calc_cropping_offsets(offsets, frame, desc);
  731. }
  732. }
  733. for (i = 0; frame->data[i]; i++)
  734. frame->data[i] += offsets[i];
  735. frame->width -= (frame->crop_left + frame->crop_right);
  736. frame->height -= (frame->crop_top + frame->crop_bottom);
  737. frame->crop_left = 0;
  738. frame->crop_right = 0;
  739. frame->crop_top = 0;
  740. frame->crop_bottom = 0;
  741. return 0;
  742. }