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