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.

599 lines
17KB

  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. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  77. int ret, i;
  78. if (!desc)
  79. return AVERROR(EINVAL);
  80. if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
  81. return ret;
  82. if (!frame->linesize[0]) {
  83. ret = av_image_fill_linesizes(frame->linesize, frame->format,
  84. frame->width);
  85. if (ret < 0)
  86. return ret;
  87. if (align <= 0)
  88. align = av_cpu_max_align();
  89. for (i = 0; i < 4 && frame->linesize[i]; i++)
  90. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  91. }
  92. for (i = 0; i < 4 && frame->linesize[i]; i++) {
  93. int h = frame->height;
  94. if (i == 1 || i == 2)
  95. h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
  96. frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h);
  97. if (!frame->buf[i])
  98. goto fail;
  99. frame->data[i] = frame->buf[i]->data;
  100. }
  101. if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  102. av_buffer_unref(&frame->buf[1]);
  103. frame->buf[1] = av_buffer_alloc(1024);
  104. if (!frame->buf[1])
  105. goto fail;
  106. frame->data[1] = frame->buf[1]->data;
  107. }
  108. frame->extended_data = frame->data;
  109. return 0;
  110. fail:
  111. av_frame_unref(frame);
  112. return AVERROR(ENOMEM);
  113. }
  114. static int get_audio_buffer(AVFrame *frame, int align)
  115. {
  116. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  117. int planar = av_sample_fmt_is_planar(frame->format);
  118. int planes = planar ? channels : 1;
  119. int ret, i;
  120. if (!frame->linesize[0]) {
  121. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  122. frame->nb_samples, frame->format,
  123. align);
  124. if (ret < 0)
  125. return ret;
  126. }
  127. if (planes > AV_NUM_DATA_POINTERS) {
  128. frame->extended_data = av_mallocz(planes *
  129. sizeof(*frame->extended_data));
  130. frame->extended_buf = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
  131. sizeof(*frame->extended_buf));
  132. if (!frame->extended_data || !frame->extended_buf) {
  133. av_freep(&frame->extended_data);
  134. av_freep(&frame->extended_buf);
  135. return AVERROR(ENOMEM);
  136. }
  137. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  138. } else
  139. frame->extended_data = frame->data;
  140. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  141. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  142. if (!frame->buf[i]) {
  143. av_frame_unref(frame);
  144. return AVERROR(ENOMEM);
  145. }
  146. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  147. }
  148. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  149. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  150. if (!frame->extended_buf[i]) {
  151. av_frame_unref(frame);
  152. return AVERROR(ENOMEM);
  153. }
  154. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  155. }
  156. return 0;
  157. }
  158. int av_frame_get_buffer(AVFrame *frame, int align)
  159. {
  160. if (frame->format < 0)
  161. return AVERROR(EINVAL);
  162. if (frame->width > 0 && frame->height > 0)
  163. return get_video_buffer(frame, align);
  164. else if (frame->nb_samples > 0 && frame->channel_layout)
  165. return get_audio_buffer(frame, align);
  166. return AVERROR(EINVAL);
  167. }
  168. int av_frame_ref(AVFrame *dst, const AVFrame *src)
  169. {
  170. int i, ret = 0;
  171. dst->format = src->format;
  172. dst->width = src->width;
  173. dst->height = src->height;
  174. dst->channel_layout = src->channel_layout;
  175. dst->nb_samples = src->nb_samples;
  176. ret = av_frame_copy_props(dst, src);
  177. if (ret < 0)
  178. return ret;
  179. /* duplicate the frame data if it's not refcounted */
  180. if (!src->buf[0]) {
  181. ret = av_frame_get_buffer(dst, 32);
  182. if (ret < 0)
  183. return ret;
  184. ret = av_frame_copy(dst, src);
  185. if (ret < 0)
  186. av_frame_unref(dst);
  187. return ret;
  188. }
  189. /* ref the buffers */
  190. for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {
  191. dst->buf[i] = av_buffer_ref(src->buf[i]);
  192. if (!dst->buf[i]) {
  193. ret = AVERROR(ENOMEM);
  194. goto fail;
  195. }
  196. }
  197. if (src->extended_buf) {
  198. dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
  199. src->nb_extended_buf);
  200. if (!dst->extended_buf) {
  201. ret = AVERROR(ENOMEM);
  202. goto fail;
  203. }
  204. dst->nb_extended_buf = src->nb_extended_buf;
  205. for (i = 0; i < src->nb_extended_buf; i++) {
  206. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  207. if (!dst->extended_buf[i]) {
  208. ret = AVERROR(ENOMEM);
  209. goto fail;
  210. }
  211. }
  212. }
  213. if (src->hw_frames_ctx) {
  214. dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
  215. if (!dst->hw_frames_ctx) {
  216. ret = AVERROR(ENOMEM);
  217. goto fail;
  218. }
  219. }
  220. /* duplicate extended data */
  221. if (src->extended_data != src->data) {
  222. int ch = av_get_channel_layout_nb_channels(src->channel_layout);
  223. if (!ch) {
  224. ret = AVERROR(EINVAL);
  225. goto fail;
  226. }
  227. dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
  228. if (!dst->extended_data) {
  229. ret = AVERROR(ENOMEM);
  230. goto fail;
  231. }
  232. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  233. } else
  234. dst->extended_data = dst->data;
  235. memcpy(dst->data, src->data, sizeof(src->data));
  236. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  237. return 0;
  238. fail:
  239. av_frame_unref(dst);
  240. return ret;
  241. }
  242. AVFrame *av_frame_clone(const AVFrame *src)
  243. {
  244. AVFrame *ret = av_frame_alloc();
  245. if (!ret)
  246. return NULL;
  247. if (av_frame_ref(ret, src) < 0)
  248. av_frame_free(&ret);
  249. return ret;
  250. }
  251. void av_frame_unref(AVFrame *frame)
  252. {
  253. int i;
  254. wipe_side_data(frame);
  255. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  256. av_buffer_unref(&frame->buf[i]);
  257. for (i = 0; i < frame->nb_extended_buf; i++)
  258. av_buffer_unref(&frame->extended_buf[i]);
  259. av_freep(&frame->extended_buf);
  260. av_buffer_unref(&frame->hw_frames_ctx);
  261. av_buffer_unref(&frame->opaque_ref);
  262. get_frame_defaults(frame);
  263. }
  264. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  265. {
  266. *dst = *src;
  267. if (src->extended_data == src->data)
  268. dst->extended_data = dst->data;
  269. memset(src, 0, sizeof(*src));
  270. get_frame_defaults(src);
  271. }
  272. int av_frame_is_writable(AVFrame *frame)
  273. {
  274. int i, ret = 1;
  275. /* assume non-refcounted frames are not writable */
  276. if (!frame->buf[0])
  277. return 0;
  278. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
  279. ret &= !!av_buffer_is_writable(frame->buf[i]);
  280. for (i = 0; i < frame->nb_extended_buf; i++)
  281. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  282. return ret;
  283. }
  284. int av_frame_make_writable(AVFrame *frame)
  285. {
  286. AVFrame tmp;
  287. int ret;
  288. if (!frame->buf[0])
  289. return AVERROR(EINVAL);
  290. if (av_frame_is_writable(frame))
  291. return 0;
  292. memset(&tmp, 0, sizeof(tmp));
  293. tmp.format = frame->format;
  294. tmp.width = frame->width;
  295. tmp.height = frame->height;
  296. tmp.channel_layout = frame->channel_layout;
  297. tmp.nb_samples = frame->nb_samples;
  298. ret = av_frame_get_buffer(&tmp, 32);
  299. if (ret < 0)
  300. return ret;
  301. ret = av_frame_copy(&tmp, frame);
  302. if (ret < 0) {
  303. av_frame_unref(&tmp);
  304. return ret;
  305. }
  306. ret = av_frame_copy_props(&tmp, frame);
  307. if (ret < 0) {
  308. av_frame_unref(&tmp);
  309. return ret;
  310. }
  311. av_frame_unref(frame);
  312. *frame = tmp;
  313. if (tmp.data == tmp.extended_data)
  314. frame->extended_data = frame->data;
  315. return 0;
  316. }
  317. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  318. {
  319. int i;
  320. dst->key_frame = src->key_frame;
  321. dst->pict_type = src->pict_type;
  322. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  323. dst->crop_top = src->crop_top;
  324. dst->crop_bottom = src->crop_bottom;
  325. dst->crop_left = src->crop_left;
  326. dst->crop_right = src->crop_right;
  327. dst->pts = src->pts;
  328. dst->repeat_pict = src->repeat_pict;
  329. dst->interlaced_frame = src->interlaced_frame;
  330. dst->top_field_first = src->top_field_first;
  331. dst->palette_has_changed = src->palette_has_changed;
  332. dst->sample_rate = src->sample_rate;
  333. dst->opaque = src->opaque;
  334. #if FF_API_PKT_PTS
  335. FF_DISABLE_DEPRECATION_WARNINGS
  336. dst->pkt_pts = src->pkt_pts;
  337. FF_ENABLE_DEPRECATION_WARNINGS
  338. #endif
  339. dst->pkt_dts = src->pkt_dts;
  340. dst->reordered_opaque = src->reordered_opaque;
  341. dst->quality = src->quality;
  342. dst->coded_picture_number = src->coded_picture_number;
  343. dst->display_picture_number = src->display_picture_number;
  344. dst->flags = src->flags;
  345. dst->color_primaries = src->color_primaries;
  346. dst->color_trc = src->color_trc;
  347. dst->colorspace = src->colorspace;
  348. dst->color_range = src->color_range;
  349. dst->chroma_location = src->chroma_location;
  350. #if FF_API_ERROR_FRAME
  351. FF_DISABLE_DEPRECATION_WARNINGS
  352. memcpy(dst->error, src->error, sizeof(dst->error));
  353. FF_ENABLE_DEPRECATION_WARNINGS
  354. #endif
  355. for (i = 0; i < src->nb_side_data; i++) {
  356. const AVFrameSideData *sd_src = src->side_data[i];
  357. AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
  358. sd_src->size);
  359. if (!sd_dst) {
  360. wipe_side_data(dst);
  361. return AVERROR(ENOMEM);
  362. }
  363. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  364. av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
  365. }
  366. av_buffer_unref(&dst->opaque_ref);
  367. if (src->opaque_ref) {
  368. dst->opaque_ref = av_buffer_ref(src->opaque_ref);
  369. if (!dst->opaque_ref)
  370. return AVERROR(ENOMEM);
  371. }
  372. return 0;
  373. }
  374. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  375. {
  376. uint8_t *data;
  377. int planes, i;
  378. if (frame->nb_samples) {
  379. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  380. if (!channels)
  381. return NULL;
  382. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  383. } else
  384. planes = 4;
  385. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  386. return NULL;
  387. data = frame->extended_data[plane];
  388. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  389. AVBufferRef *buf = frame->buf[i];
  390. if (data >= buf->data && data < buf->data + buf->size)
  391. return buf;
  392. }
  393. for (i = 0; i < frame->nb_extended_buf; i++) {
  394. AVBufferRef *buf = frame->extended_buf[i];
  395. if (data >= buf->data && data < buf->data + buf->size)
  396. return buf;
  397. }
  398. return NULL;
  399. }
  400. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  401. enum AVFrameSideDataType type,
  402. int size)
  403. {
  404. AVFrameSideData *ret, **tmp;
  405. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  406. return NULL;
  407. tmp = av_realloc(frame->side_data,
  408. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  409. if (!tmp)
  410. return NULL;
  411. frame->side_data = tmp;
  412. ret = av_mallocz(sizeof(*ret));
  413. if (!ret)
  414. return NULL;
  415. ret->data = av_malloc(size);
  416. if (!ret->data) {
  417. av_freep(&ret);
  418. return NULL;
  419. }
  420. ret->size = size;
  421. ret->type = type;
  422. frame->side_data[frame->nb_side_data++] = ret;
  423. return ret;
  424. }
  425. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  426. enum AVFrameSideDataType type)
  427. {
  428. int i;
  429. for (i = 0; i < frame->nb_side_data; i++) {
  430. if (frame->side_data[i]->type == type)
  431. return frame->side_data[i];
  432. }
  433. return NULL;
  434. }
  435. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  436. {
  437. const uint8_t *src_data[4];
  438. int i, planes;
  439. if (dst->width != src->width ||
  440. dst->height != src->height)
  441. return AVERROR(EINVAL);
  442. planes = av_pix_fmt_count_planes(dst->format);
  443. for (i = 0; i < planes; i++)
  444. if (!dst->data[i] || !src->data[i])
  445. return AVERROR(EINVAL);
  446. memcpy(src_data, src->data, sizeof(src_data));
  447. av_image_copy(dst->data, dst->linesize,
  448. src_data, src->linesize,
  449. dst->format, dst->width, dst->height);
  450. return 0;
  451. }
  452. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  453. {
  454. int planar = av_sample_fmt_is_planar(dst->format);
  455. int channels = av_get_channel_layout_nb_channels(dst->channel_layout);
  456. int planes = planar ? channels : 1;
  457. int i;
  458. if (dst->nb_samples != src->nb_samples ||
  459. dst->channel_layout != src->channel_layout)
  460. return AVERROR(EINVAL);
  461. for (i = 0; i < planes; i++)
  462. if (!dst->extended_data[i] || !src->extended_data[i])
  463. return AVERROR(EINVAL);
  464. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  465. dst->nb_samples, channels, dst->format);
  466. return 0;
  467. }
  468. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  469. {
  470. if (dst->format != src->format || dst->format < 0)
  471. return AVERROR(EINVAL);
  472. if (dst->width > 0 && dst->height > 0)
  473. return frame_copy_video(dst, src);
  474. else if (dst->nb_samples > 0 && dst->channel_layout)
  475. return frame_copy_audio(dst, src);
  476. return AVERROR(EINVAL);
  477. }
  478. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  479. {
  480. int i;
  481. for (i = 0; i < frame->nb_side_data; i++) {
  482. AVFrameSideData *sd = frame->side_data[i];
  483. if (sd->type == type) {
  484. free_side_data(&frame->side_data[i]);
  485. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  486. frame->nb_side_data--;
  487. }
  488. }
  489. }