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.

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