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.

888 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. 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. return 0;
  337. }
  338. int av_frame_ref(AVFrame *dst, const AVFrame *src)
  339. {
  340. int i, ret = 0;
  341. av_assert1(dst->width == 0 && dst->height == 0);
  342. av_assert1(dst->channels == 0);
  343. dst->format = src->format;
  344. dst->width = src->width;
  345. dst->height = src->height;
  346. dst->channels = src->channels;
  347. dst->channel_layout = src->channel_layout;
  348. dst->nb_samples = src->nb_samples;
  349. ret = frame_copy_props(dst, src, 0);
  350. if (ret < 0)
  351. return ret;
  352. /* duplicate the frame data if it's not refcounted */
  353. if (!src->buf[0]) {
  354. ret = av_frame_get_buffer(dst, 32);
  355. if (ret < 0)
  356. return ret;
  357. ret = av_frame_copy(dst, src);
  358. if (ret < 0)
  359. av_frame_unref(dst);
  360. return ret;
  361. }
  362. /* ref the buffers */
  363. for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
  364. if (!src->buf[i])
  365. continue;
  366. dst->buf[i] = av_buffer_ref(src->buf[i]);
  367. if (!dst->buf[i]) {
  368. ret = AVERROR(ENOMEM);
  369. goto fail;
  370. }
  371. }
  372. if (src->extended_buf) {
  373. dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
  374. src->nb_extended_buf);
  375. if (!dst->extended_buf) {
  376. ret = AVERROR(ENOMEM);
  377. goto fail;
  378. }
  379. dst->nb_extended_buf = src->nb_extended_buf;
  380. for (i = 0; i < src->nb_extended_buf; i++) {
  381. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  382. if (!dst->extended_buf[i]) {
  383. ret = AVERROR(ENOMEM);
  384. goto fail;
  385. }
  386. }
  387. }
  388. if (src->hw_frames_ctx) {
  389. dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
  390. if (!dst->hw_frames_ctx) {
  391. ret = AVERROR(ENOMEM);
  392. goto fail;
  393. }
  394. }
  395. /* duplicate extended data */
  396. if (src->extended_data != src->data) {
  397. int ch = src->channels;
  398. if (!ch) {
  399. ret = AVERROR(EINVAL);
  400. goto fail;
  401. }
  402. CHECK_CHANNELS_CONSISTENCY(src);
  403. dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
  404. if (!dst->extended_data) {
  405. ret = AVERROR(ENOMEM);
  406. goto fail;
  407. }
  408. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  409. } else
  410. dst->extended_data = dst->data;
  411. memcpy(dst->data, src->data, sizeof(src->data));
  412. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  413. return 0;
  414. fail:
  415. av_frame_unref(dst);
  416. return ret;
  417. }
  418. AVFrame *av_frame_clone(const AVFrame *src)
  419. {
  420. AVFrame *ret = av_frame_alloc();
  421. if (!ret)
  422. return NULL;
  423. if (av_frame_ref(ret, src) < 0)
  424. av_frame_free(&ret);
  425. return ret;
  426. }
  427. void av_frame_unref(AVFrame *frame)
  428. {
  429. int i;
  430. if (!frame)
  431. return;
  432. wipe_side_data(frame);
  433. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  434. av_buffer_unref(&frame->buf[i]);
  435. for (i = 0; i < frame->nb_extended_buf; i++)
  436. av_buffer_unref(&frame->extended_buf[i]);
  437. av_freep(&frame->extended_buf);
  438. av_dict_free(&frame->metadata);
  439. #if FF_API_FRAME_QP
  440. av_buffer_unref(&frame->qp_table_buf);
  441. #endif
  442. av_buffer_unref(&frame->hw_frames_ctx);
  443. av_buffer_unref(&frame->opaque_ref);
  444. get_frame_defaults(frame);
  445. }
  446. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  447. {
  448. av_assert1(dst->width == 0 && dst->height == 0);
  449. av_assert1(dst->channels == 0);
  450. *dst = *src;
  451. if (src->extended_data == src->data)
  452. dst->extended_data = dst->data;
  453. memset(src, 0, sizeof(*src));
  454. get_frame_defaults(src);
  455. }
  456. int av_frame_is_writable(AVFrame *frame)
  457. {
  458. int i, ret = 1;
  459. /* assume non-refcounted frames are not writable */
  460. if (!frame->buf[0])
  461. return 0;
  462. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  463. if (frame->buf[i])
  464. ret &= !!av_buffer_is_writable(frame->buf[i]);
  465. for (i = 0; i < frame->nb_extended_buf; i++)
  466. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  467. return ret;
  468. }
  469. int av_frame_make_writable(AVFrame *frame)
  470. {
  471. AVFrame tmp;
  472. int ret;
  473. if (!frame->buf[0])
  474. return AVERROR(EINVAL);
  475. if (av_frame_is_writable(frame))
  476. return 0;
  477. memset(&tmp, 0, sizeof(tmp));
  478. tmp.format = frame->format;
  479. tmp.width = frame->width;
  480. tmp.height = frame->height;
  481. tmp.channels = frame->channels;
  482. tmp.channel_layout = frame->channel_layout;
  483. tmp.nb_samples = frame->nb_samples;
  484. ret = av_frame_get_buffer(&tmp, 32);
  485. if (ret < 0)
  486. return ret;
  487. ret = av_frame_copy(&tmp, frame);
  488. if (ret < 0) {
  489. av_frame_unref(&tmp);
  490. return ret;
  491. }
  492. ret = av_frame_copy_props(&tmp, frame);
  493. if (ret < 0) {
  494. av_frame_unref(&tmp);
  495. return ret;
  496. }
  497. av_frame_unref(frame);
  498. *frame = tmp;
  499. if (tmp.data == tmp.extended_data)
  500. frame->extended_data = frame->data;
  501. return 0;
  502. }
  503. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  504. {
  505. return frame_copy_props(dst, src, 1);
  506. }
  507. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  508. {
  509. uint8_t *data;
  510. int planes, i;
  511. if (frame->nb_samples) {
  512. int channels = frame->channels;
  513. if (!channels)
  514. return NULL;
  515. CHECK_CHANNELS_CONSISTENCY(frame);
  516. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  517. } else
  518. planes = 4;
  519. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  520. return NULL;
  521. data = frame->extended_data[plane];
  522. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  523. AVBufferRef *buf = frame->buf[i];
  524. if (data >= buf->data && data < buf->data + buf->size)
  525. return buf;
  526. }
  527. for (i = 0; i < frame->nb_extended_buf; i++) {
  528. AVBufferRef *buf = frame->extended_buf[i];
  529. if (data >= buf->data && data < buf->data + buf->size)
  530. return buf;
  531. }
  532. return NULL;
  533. }
  534. static AVFrameSideData *frame_new_side_data(AVFrame *frame,
  535. enum AVFrameSideDataType type,
  536. AVBufferRef *buf)
  537. {
  538. AVFrameSideData *ret, **tmp;
  539. if (!buf)
  540. return NULL;
  541. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  542. goto fail;
  543. tmp = av_realloc(frame->side_data,
  544. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  545. if (!tmp)
  546. goto fail;
  547. frame->side_data = tmp;
  548. ret = av_mallocz(sizeof(*ret));
  549. if (!ret)
  550. goto fail;
  551. ret->buf = buf;
  552. ret->data = ret->buf->data;
  553. ret->size = buf->size;
  554. ret->type = type;
  555. frame->side_data[frame->nb_side_data++] = ret;
  556. return ret;
  557. fail:
  558. av_buffer_unref(&buf);
  559. return NULL;
  560. }
  561. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  562. enum AVFrameSideDataType type,
  563. int size)
  564. {
  565. return frame_new_side_data(frame, type, av_buffer_alloc(size));
  566. }
  567. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  568. enum AVFrameSideDataType type)
  569. {
  570. int i;
  571. for (i = 0; i < frame->nb_side_data; i++) {
  572. if (frame->side_data[i]->type == type)
  573. return frame->side_data[i];
  574. }
  575. return NULL;
  576. }
  577. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  578. {
  579. const uint8_t *src_data[4];
  580. int i, planes;
  581. if (dst->width < src->width ||
  582. dst->height < src->height)
  583. return AVERROR(EINVAL);
  584. planes = av_pix_fmt_count_planes(dst->format);
  585. for (i = 0; i < planes; i++)
  586. if (!dst->data[i] || !src->data[i])
  587. return AVERROR(EINVAL);
  588. memcpy(src_data, src->data, sizeof(src_data));
  589. av_image_copy(dst->data, dst->linesize,
  590. src_data, src->linesize,
  591. dst->format, src->width, src->height);
  592. return 0;
  593. }
  594. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  595. {
  596. int planar = av_sample_fmt_is_planar(dst->format);
  597. int channels = dst->channels;
  598. int planes = planar ? channels : 1;
  599. int i;
  600. if (dst->nb_samples != src->nb_samples ||
  601. dst->channels != src->channels ||
  602. dst->channel_layout != src->channel_layout)
  603. return AVERROR(EINVAL);
  604. CHECK_CHANNELS_CONSISTENCY(src);
  605. for (i = 0; i < planes; i++)
  606. if (!dst->extended_data[i] || !src->extended_data[i])
  607. return AVERROR(EINVAL);
  608. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  609. dst->nb_samples, channels, dst->format);
  610. return 0;
  611. }
  612. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  613. {
  614. if (dst->format != src->format || dst->format < 0)
  615. return AVERROR(EINVAL);
  616. if (dst->width > 0 && dst->height > 0)
  617. return frame_copy_video(dst, src);
  618. else if (dst->nb_samples > 0 && dst->channels > 0)
  619. return frame_copy_audio(dst, src);
  620. return AVERROR(EINVAL);
  621. }
  622. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  623. {
  624. int i;
  625. for (i = 0; i < frame->nb_side_data; i++) {
  626. AVFrameSideData *sd = frame->side_data[i];
  627. if (sd->type == type) {
  628. free_side_data(&frame->side_data[i]);
  629. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  630. frame->nb_side_data--;
  631. }
  632. }
  633. }
  634. const char *av_frame_side_data_name(enum AVFrameSideDataType type)
  635. {
  636. switch(type) {
  637. case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
  638. case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
  639. case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
  640. case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
  641. case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
  642. case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
  643. case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
  644. case AV_FRAME_DATA_AFD: return "Active format description";
  645. case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
  646. case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
  647. case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
  648. case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
  649. case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
  650. case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";
  651. case AV_FRAME_DATA_ICC_PROFILE: return "ICC profile";
  652. }
  653. return NULL;
  654. }
  655. static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
  656. const AVPixFmtDescriptor *desc)
  657. {
  658. int i, j;
  659. for (i = 0; frame->data[i]; i++) {
  660. const AVComponentDescriptor *comp = NULL;
  661. int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
  662. int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  663. if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL) && i == 1) {
  664. offsets[i] = 0;
  665. break;
  666. }
  667. /* find any component descriptor for this plane */
  668. for (j = 0; j < desc->nb_components; j++) {
  669. if (desc->comp[j].plane == i) {
  670. comp = &desc->comp[j];
  671. break;
  672. }
  673. }
  674. if (!comp)
  675. return AVERROR_BUG;
  676. offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] +
  677. (frame->crop_left >> shift_x) * comp->step;
  678. }
  679. return 0;
  680. }
  681. int av_frame_apply_cropping(AVFrame *frame, int flags)
  682. {
  683. const AVPixFmtDescriptor *desc;
  684. size_t offsets[4];
  685. int i;
  686. if (!(frame->width > 0 && frame->height > 0))
  687. return AVERROR(EINVAL);
  688. if (frame->crop_left >= INT_MAX - frame->crop_right ||
  689. frame->crop_top >= INT_MAX - frame->crop_bottom ||
  690. (frame->crop_left + frame->crop_right) >= frame->width ||
  691. (frame->crop_top + frame->crop_bottom) >= frame->height)
  692. return AVERROR(ERANGE);
  693. desc = av_pix_fmt_desc_get(frame->format);
  694. if (!desc)
  695. return AVERROR_BUG;
  696. /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
  697. * formats cannot be easily handled here either (and corresponding decoders
  698. * should not export any cropping anyway), so do the same for those as well.
  699. * */
  700. if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
  701. frame->width -= frame->crop_right;
  702. frame->height -= frame->crop_bottom;
  703. frame->crop_right = 0;
  704. frame->crop_bottom = 0;
  705. return 0;
  706. }
  707. /* calculate the offsets for each plane */
  708. calc_cropping_offsets(offsets, frame, desc);
  709. /* adjust the offsets to avoid breaking alignment */
  710. if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
  711. int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
  712. int min_log2_align = INT_MAX;
  713. for (i = 0; frame->data[i]; i++) {
  714. int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
  715. min_log2_align = FFMIN(log2_align, min_log2_align);
  716. }
  717. /* we assume, and it should always be true, that the data alignment is
  718. * related to the cropping alignment by a constant power-of-2 factor */
  719. if (log2_crop_align < min_log2_align)
  720. return AVERROR_BUG;
  721. if (min_log2_align < 5) {
  722. frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
  723. calc_cropping_offsets(offsets, frame, desc);
  724. }
  725. }
  726. for (i = 0; frame->data[i]; i++)
  727. frame->data[i] += offsets[i];
  728. frame->width -= (frame->crop_left + frame->crop_right);
  729. frame->height -= (frame->crop_top + frame->crop_bottom);
  730. frame->crop_left = 0;
  731. frame->crop_right = 0;
  732. frame->crop_top = 0;
  733. frame->crop_bottom = 0;
  734. return 0;
  735. }