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.

944 lines
28KB

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