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.

582 lines
16KB

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