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.

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