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.

688 lines
20KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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 "buffer.h"
  20. #include "common.h"
  21. #include "cpu.h"
  22. #include "dict.h"
  23. #include "frame.h"
  24. #include "imgutils.h"
  25. #include "mem.h"
  26. #include "samplefmt.h"
  27. static void get_frame_defaults(AVFrame *frame)
  28. {
  29. if (frame->extended_data != frame->data)
  30. av_freep(&frame->extended_data);
  31. memset(frame, 0, sizeof(*frame));
  32. frame->pts = AV_NOPTS_VALUE;
  33. frame->key_frame = 1;
  34. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  35. frame->format = -1; /* unknown */
  36. frame->extended_data = frame->data;
  37. frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
  38. frame->color_trc = AVCOL_TRC_UNSPECIFIED;
  39. frame->colorspace = AVCOL_SPC_UNSPECIFIED;
  40. frame->color_range = AVCOL_RANGE_UNSPECIFIED;
  41. frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  42. }
  43. static void free_side_data(AVFrameSideData **ptr_sd)
  44. {
  45. AVFrameSideData *sd = *ptr_sd;
  46. av_freep(&sd->data);
  47. av_dict_free(&sd->metadata);
  48. av_freep(ptr_sd);
  49. }
  50. static void wipe_side_data(AVFrame *frame)
  51. {
  52. int i;
  53. for (i = 0; i < frame->nb_side_data; i++) {
  54. free_side_data(&frame->side_data[i]);
  55. }
  56. frame->nb_side_data = 0;
  57. av_freep(&frame->side_data);
  58. }
  59. AVFrame *av_frame_alloc(void)
  60. {
  61. AVFrame *frame = av_mallocz(sizeof(*frame));
  62. if (!frame)
  63. return NULL;
  64. get_frame_defaults(frame);
  65. return frame;
  66. }
  67. void av_frame_free(AVFrame **frame)
  68. {
  69. if (!frame || !*frame)
  70. return;
  71. av_frame_unref(*frame);
  72. av_freep(frame);
  73. }
  74. static int get_video_buffer(AVFrame *frame, int align)
  75. {
  76. int ret, i;
  77. if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
  78. return ret;
  79. if (!frame->linesize[0]) {
  80. ret = av_image_fill_linesizes(frame->linesize, frame->format,
  81. frame->width);
  82. if (ret < 0)
  83. return ret;
  84. if (align <= 0)
  85. align = av_cpu_max_align();
  86. for (i = 0; i < 4 && frame->linesize[i]; i++)
  87. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  88. }
  89. if ((ret = av_image_fill_pointers(frame->data, frame->format, frame->height,
  90. NULL, frame->linesize)) < 0)
  91. return ret;
  92. frame->buf[0] = av_buffer_alloc(ret);
  93. if (!frame->buf[0])
  94. goto fail;
  95. if (av_image_fill_pointers(frame->data, frame->format, frame->height,
  96. frame->buf[0]->data, frame->linesize) < 0)
  97. goto fail;
  98. frame->extended_data = frame->data;
  99. return 0;
  100. fail:
  101. av_frame_unref(frame);
  102. return AVERROR(ENOMEM);
  103. }
  104. static int get_audio_buffer(AVFrame *frame, int align)
  105. {
  106. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  107. int planar = av_sample_fmt_is_planar(frame->format);
  108. int planes = planar ? channels : 1;
  109. int ret, i;
  110. if (!frame->linesize[0]) {
  111. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  112. frame->nb_samples, frame->format,
  113. align);
  114. if (ret < 0)
  115. return ret;
  116. }
  117. if (planes > AV_NUM_DATA_POINTERS) {
  118. frame->extended_data = av_mallocz(planes *
  119. sizeof(*frame->extended_data));
  120. frame->extended_buf = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
  121. sizeof(*frame->extended_buf));
  122. if (!frame->extended_data || !frame->extended_buf) {
  123. av_freep(&frame->extended_data);
  124. av_freep(&frame->extended_buf);
  125. return AVERROR(ENOMEM);
  126. }
  127. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  128. } else
  129. frame->extended_data = frame->data;
  130. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  131. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  132. if (!frame->buf[i]) {
  133. av_frame_unref(frame);
  134. return AVERROR(ENOMEM);
  135. }
  136. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  137. }
  138. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  139. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  140. if (!frame->extended_buf[i]) {
  141. av_frame_unref(frame);
  142. return AVERROR(ENOMEM);
  143. }
  144. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  145. }
  146. return 0;
  147. }
  148. int av_frame_get_buffer(AVFrame *frame, int align)
  149. {
  150. if (frame->format < 0)
  151. return AVERROR(EINVAL);
  152. if (frame->width > 0 && frame->height > 0)
  153. return get_video_buffer(frame, align);
  154. else if (frame->nb_samples > 0 && frame->channel_layout)
  155. return get_audio_buffer(frame, align);
  156. return AVERROR(EINVAL);
  157. }
  158. int av_frame_ref(AVFrame *dst, const AVFrame *src)
  159. {
  160. int i, ret = 0;
  161. dst->format = src->format;
  162. dst->width = src->width;
  163. dst->height = src->height;
  164. dst->channel_layout = src->channel_layout;
  165. dst->nb_samples = src->nb_samples;
  166. ret = av_frame_copy_props(dst, src);
  167. if (ret < 0)
  168. return ret;
  169. /* duplicate the frame data if it's not refcounted */
  170. if (!src->buf[0]) {
  171. ret = av_frame_get_buffer(dst, 32);
  172. if (ret < 0)
  173. return ret;
  174. ret = av_frame_copy(dst, src);
  175. if (ret < 0)
  176. av_frame_unref(dst);
  177. return ret;
  178. }
  179. /* ref the buffers */
  180. for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {
  181. dst->buf[i] = av_buffer_ref(src->buf[i]);
  182. if (!dst->buf[i]) {
  183. ret = AVERROR(ENOMEM);
  184. goto fail;
  185. }
  186. }
  187. if (src->extended_buf) {
  188. dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
  189. src->nb_extended_buf);
  190. if (!dst->extended_buf) {
  191. ret = AVERROR(ENOMEM);
  192. goto fail;
  193. }
  194. dst->nb_extended_buf = src->nb_extended_buf;
  195. for (i = 0; i < src->nb_extended_buf; i++) {
  196. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  197. if (!dst->extended_buf[i]) {
  198. ret = AVERROR(ENOMEM);
  199. goto fail;
  200. }
  201. }
  202. }
  203. if (src->hw_frames_ctx) {
  204. dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
  205. if (!dst->hw_frames_ctx) {
  206. ret = AVERROR(ENOMEM);
  207. goto fail;
  208. }
  209. }
  210. /* duplicate extended data */
  211. if (src->extended_data != src->data) {
  212. int ch = av_get_channel_layout_nb_channels(src->channel_layout);
  213. if (!ch) {
  214. ret = AVERROR(EINVAL);
  215. goto fail;
  216. }
  217. dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
  218. if (!dst->extended_data) {
  219. ret = AVERROR(ENOMEM);
  220. goto fail;
  221. }
  222. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  223. } else
  224. dst->extended_data = dst->data;
  225. memcpy(dst->data, src->data, sizeof(src->data));
  226. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  227. return 0;
  228. fail:
  229. av_frame_unref(dst);
  230. return ret;
  231. }
  232. AVFrame *av_frame_clone(const AVFrame *src)
  233. {
  234. AVFrame *ret = av_frame_alloc();
  235. if (!ret)
  236. return NULL;
  237. if (av_frame_ref(ret, src) < 0)
  238. av_frame_free(&ret);
  239. return ret;
  240. }
  241. void av_frame_unref(AVFrame *frame)
  242. {
  243. int i;
  244. wipe_side_data(frame);
  245. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  246. av_buffer_unref(&frame->buf[i]);
  247. for (i = 0; i < frame->nb_extended_buf; i++)
  248. av_buffer_unref(&frame->extended_buf[i]);
  249. av_freep(&frame->extended_buf);
  250. av_buffer_unref(&frame->hw_frames_ctx);
  251. av_buffer_unref(&frame->opaque_ref);
  252. get_frame_defaults(frame);
  253. }
  254. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  255. {
  256. *dst = *src;
  257. if (src->extended_data == src->data)
  258. dst->extended_data = dst->data;
  259. memset(src, 0, sizeof(*src));
  260. get_frame_defaults(src);
  261. }
  262. int av_frame_is_writable(AVFrame *frame)
  263. {
  264. int i, ret = 1;
  265. /* assume non-refcounted frames are not writable */
  266. if (!frame->buf[0])
  267. return 0;
  268. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
  269. ret &= !!av_buffer_is_writable(frame->buf[i]);
  270. for (i = 0; i < frame->nb_extended_buf; i++)
  271. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  272. return ret;
  273. }
  274. int av_frame_make_writable(AVFrame *frame)
  275. {
  276. AVFrame tmp;
  277. int ret;
  278. if (!frame->buf[0])
  279. return AVERROR(EINVAL);
  280. if (av_frame_is_writable(frame))
  281. return 0;
  282. memset(&tmp, 0, sizeof(tmp));
  283. tmp.format = frame->format;
  284. tmp.width = frame->width;
  285. tmp.height = frame->height;
  286. tmp.channel_layout = frame->channel_layout;
  287. tmp.nb_samples = frame->nb_samples;
  288. ret = av_frame_get_buffer(&tmp, 32);
  289. if (ret < 0)
  290. return ret;
  291. ret = av_frame_copy(&tmp, frame);
  292. if (ret < 0) {
  293. av_frame_unref(&tmp);
  294. return ret;
  295. }
  296. ret = av_frame_copy_props(&tmp, frame);
  297. if (ret < 0) {
  298. av_frame_unref(&tmp);
  299. return ret;
  300. }
  301. av_frame_unref(frame);
  302. *frame = tmp;
  303. if (tmp.data == tmp.extended_data)
  304. frame->extended_data = frame->data;
  305. return 0;
  306. }
  307. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  308. {
  309. int i;
  310. dst->key_frame = src->key_frame;
  311. dst->pict_type = src->pict_type;
  312. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  313. dst->crop_top = src->crop_top;
  314. dst->crop_bottom = src->crop_bottom;
  315. dst->crop_left = src->crop_left;
  316. dst->crop_right = src->crop_right;
  317. dst->pts = src->pts;
  318. dst->repeat_pict = src->repeat_pict;
  319. dst->interlaced_frame = src->interlaced_frame;
  320. dst->top_field_first = src->top_field_first;
  321. dst->palette_has_changed = src->palette_has_changed;
  322. dst->sample_rate = src->sample_rate;
  323. dst->opaque = src->opaque;
  324. #if FF_API_PKT_PTS
  325. FF_DISABLE_DEPRECATION_WARNINGS
  326. dst->pkt_pts = src->pkt_pts;
  327. FF_ENABLE_DEPRECATION_WARNINGS
  328. #endif
  329. dst->pkt_dts = src->pkt_dts;
  330. dst->reordered_opaque = src->reordered_opaque;
  331. dst->quality = src->quality;
  332. dst->coded_picture_number = src->coded_picture_number;
  333. dst->display_picture_number = src->display_picture_number;
  334. dst->flags = src->flags;
  335. dst->color_primaries = src->color_primaries;
  336. dst->color_trc = src->color_trc;
  337. dst->colorspace = src->colorspace;
  338. dst->color_range = src->color_range;
  339. dst->chroma_location = src->chroma_location;
  340. #if FF_API_ERROR_FRAME
  341. FF_DISABLE_DEPRECATION_WARNINGS
  342. memcpy(dst->error, src->error, sizeof(dst->error));
  343. FF_ENABLE_DEPRECATION_WARNINGS
  344. #endif
  345. for (i = 0; i < src->nb_side_data; i++) {
  346. const AVFrameSideData *sd_src = src->side_data[i];
  347. AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
  348. sd_src->size);
  349. if (!sd_dst) {
  350. wipe_side_data(dst);
  351. return AVERROR(ENOMEM);
  352. }
  353. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  354. av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
  355. }
  356. av_buffer_unref(&dst->opaque_ref);
  357. if (src->opaque_ref) {
  358. dst->opaque_ref = av_buffer_ref(src->opaque_ref);
  359. if (!dst->opaque_ref)
  360. return AVERROR(ENOMEM);
  361. }
  362. return 0;
  363. }
  364. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  365. {
  366. uint8_t *data;
  367. int planes, i;
  368. if (frame->nb_samples) {
  369. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  370. if (!channels)
  371. return NULL;
  372. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  373. } else
  374. planes = 4;
  375. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  376. return NULL;
  377. data = frame->extended_data[plane];
  378. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  379. AVBufferRef *buf = frame->buf[i];
  380. if (data >= buf->data && data < buf->data + buf->size)
  381. return buf;
  382. }
  383. for (i = 0; i < frame->nb_extended_buf; i++) {
  384. AVBufferRef *buf = frame->extended_buf[i];
  385. if (data >= buf->data && data < buf->data + buf->size)
  386. return buf;
  387. }
  388. return NULL;
  389. }
  390. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  391. enum AVFrameSideDataType type,
  392. int size)
  393. {
  394. AVFrameSideData *ret, **tmp;
  395. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  396. return NULL;
  397. tmp = av_realloc(frame->side_data,
  398. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  399. if (!tmp)
  400. return NULL;
  401. frame->side_data = tmp;
  402. ret = av_mallocz(sizeof(*ret));
  403. if (!ret)
  404. return NULL;
  405. ret->data = av_malloc(size);
  406. if (!ret->data) {
  407. av_freep(&ret);
  408. return NULL;
  409. }
  410. ret->size = size;
  411. ret->type = type;
  412. frame->side_data[frame->nb_side_data++] = ret;
  413. return ret;
  414. }
  415. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  416. enum AVFrameSideDataType type)
  417. {
  418. int i;
  419. for (i = 0; i < frame->nb_side_data; i++) {
  420. if (frame->side_data[i]->type == type)
  421. return frame->side_data[i];
  422. }
  423. return NULL;
  424. }
  425. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  426. {
  427. const uint8_t *src_data[4];
  428. int i, planes;
  429. if (dst->width != src->width ||
  430. dst->height != src->height)
  431. return AVERROR(EINVAL);
  432. planes = av_pix_fmt_count_planes(dst->format);
  433. for (i = 0; i < planes; i++)
  434. if (!dst->data[i] || !src->data[i])
  435. return AVERROR(EINVAL);
  436. memcpy(src_data, src->data, sizeof(src_data));
  437. av_image_copy(dst->data, dst->linesize,
  438. src_data, src->linesize,
  439. dst->format, dst->width, dst->height);
  440. return 0;
  441. }
  442. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  443. {
  444. int planar = av_sample_fmt_is_planar(dst->format);
  445. int channels = av_get_channel_layout_nb_channels(dst->channel_layout);
  446. int planes = planar ? channels : 1;
  447. int i;
  448. if (dst->nb_samples != src->nb_samples ||
  449. dst->channel_layout != src->channel_layout)
  450. return AVERROR(EINVAL);
  451. for (i = 0; i < planes; i++)
  452. if (!dst->extended_data[i] || !src->extended_data[i])
  453. return AVERROR(EINVAL);
  454. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  455. dst->nb_samples, channels, dst->format);
  456. return 0;
  457. }
  458. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  459. {
  460. if (dst->format != src->format || dst->format < 0)
  461. return AVERROR(EINVAL);
  462. if (dst->width > 0 && dst->height > 0)
  463. return frame_copy_video(dst, src);
  464. else if (dst->nb_samples > 0 && dst->channel_layout)
  465. return frame_copy_audio(dst, src);
  466. return AVERROR(EINVAL);
  467. }
  468. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  469. {
  470. int i;
  471. for (i = 0; i < frame->nb_side_data; i++) {
  472. AVFrameSideData *sd = frame->side_data[i];
  473. if (sd->type == type) {
  474. free_side_data(&frame->side_data[i]);
  475. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  476. frame->nb_side_data--;
  477. }
  478. }
  479. }
  480. static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
  481. const AVPixFmtDescriptor *desc)
  482. {
  483. int i, j;
  484. for (i = 0; frame->data[i]; i++) {
  485. const AVComponentDescriptor *comp = NULL;
  486. int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
  487. int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  488. if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL) && i == 1) {
  489. offsets[i] = 0;
  490. break;
  491. }
  492. /* find any component descriptor for this plane */
  493. for (j = 0; j < desc->nb_components; j++) {
  494. if (desc->comp[j].plane == i) {
  495. comp = &desc->comp[j];
  496. break;
  497. }
  498. }
  499. if (!comp)
  500. return AVERROR_BUG;
  501. offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] +
  502. (frame->crop_left >> shift_x) * comp->step;
  503. }
  504. return 0;
  505. }
  506. int av_frame_apply_cropping(AVFrame *frame, int flags)
  507. {
  508. const AVPixFmtDescriptor *desc;
  509. size_t offsets[4];
  510. int i;
  511. if (!(frame->width > 0 && frame->height > 0))
  512. return AVERROR(EINVAL);
  513. if (frame->crop_left >= INT_MAX - frame->crop_right ||
  514. frame->crop_top >= INT_MAX - frame->crop_bottom ||
  515. (frame->crop_left + frame->crop_right) >= frame->width ||
  516. (frame->crop_top + frame->crop_bottom) >= frame->height)
  517. return AVERROR(ERANGE);
  518. desc = av_pix_fmt_desc_get(frame->format);
  519. if (!desc)
  520. return AVERROR_BUG;
  521. /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
  522. * formats cannot be easily handled here either (and corresponding decoders
  523. * should not export any cropping anyway), so do the same for those as well.
  524. * */
  525. if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
  526. frame->width -= frame->crop_right;
  527. frame->height -= frame->crop_bottom;
  528. frame->crop_right = 0;
  529. frame->crop_bottom = 0;
  530. return 0;
  531. }
  532. /* calculate the offsets for each plane */
  533. calc_cropping_offsets(offsets, frame, desc);
  534. /* adjust the offsets to avoid breaking alignment */
  535. if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
  536. int log2_crop_align = frame->crop_left ? av_ctz(frame->crop_left) : INT_MAX;
  537. int min_log2_align = INT_MAX;
  538. for (i = 0; frame->data[i]; i++) {
  539. int log2_align = offsets[i] ? av_ctz(offsets[i]) : INT_MAX;
  540. min_log2_align = FFMIN(log2_align, min_log2_align);
  541. }
  542. /* we assume, and it should always be true, that the data alignment is
  543. * related to the cropping alignment by a constant power-of-2 factor */
  544. if (log2_crop_align < min_log2_align)
  545. return AVERROR_BUG;
  546. if (min_log2_align < 5) {
  547. frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
  548. calc_cropping_offsets(offsets, frame, desc);
  549. }
  550. }
  551. for (i = 0; frame->data[i]; i++)
  552. frame->data[i] += offsets[i];
  553. frame->width -= (frame->crop_left + frame->crop_right);
  554. frame->height -= (frame->crop_top + frame->crop_bottom);
  555. frame->crop_left = 0;
  556. frame->crop_right = 0;
  557. frame->crop_top = 0;
  558. frame->crop_bottom = 0;
  559. return 0;
  560. }