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.

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