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.

564 lines
16KB

  1. /*
  2. *
  3. * This file is part of Libav.
  4. *
  5. * Libav is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * Libav is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with Libav; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "channel_layout.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 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. for (i = 0; i < 4 && frame->linesize[i]; i++)
  88. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  89. }
  90. for (i = 0; i < 4 && frame->linesize[i]; i++) {
  91. int h = frame->height;
  92. if (i == 1 || i == 2)
  93. h = -((-h) >> desc->log2_chroma_h);
  94. frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h);
  95. if (!frame->buf[i])
  96. goto fail;
  97. frame->data[i] = frame->buf[i]->data;
  98. }
  99. if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  100. av_buffer_unref(&frame->buf[1]);
  101. frame->buf[1] = av_buffer_alloc(1024);
  102. if (!frame->buf[1])
  103. goto fail;
  104. frame->data[1] = frame->buf[1]->data;
  105. }
  106. frame->extended_data = frame->data;
  107. return 0;
  108. fail:
  109. av_frame_unref(frame);
  110. return AVERROR(ENOMEM);
  111. }
  112. static int get_audio_buffer(AVFrame *frame, int align)
  113. {
  114. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  115. int planar = av_sample_fmt_is_planar(frame->format);
  116. int planes = planar ? channels : 1;
  117. int ret, i;
  118. if (!frame->linesize[0]) {
  119. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  120. frame->nb_samples, frame->format,
  121. align);
  122. if (ret < 0)
  123. return ret;
  124. }
  125. if (planes > AV_NUM_DATA_POINTERS) {
  126. frame->extended_data = av_mallocz(planes *
  127. sizeof(*frame->extended_data));
  128. frame->extended_buf = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
  129. sizeof(*frame->extended_buf));
  130. if (!frame->extended_data || !frame->extended_buf) {
  131. av_freep(&frame->extended_data);
  132. av_freep(&frame->extended_buf);
  133. return AVERROR(ENOMEM);
  134. }
  135. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  136. } else
  137. frame->extended_data = frame->data;
  138. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  139. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  140. if (!frame->buf[i]) {
  141. av_frame_unref(frame);
  142. return AVERROR(ENOMEM);
  143. }
  144. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  145. }
  146. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  147. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  148. if (!frame->extended_buf[i]) {
  149. av_frame_unref(frame);
  150. return AVERROR(ENOMEM);
  151. }
  152. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  153. }
  154. return 0;
  155. }
  156. int av_frame_get_buffer(AVFrame *frame, int align)
  157. {
  158. if (frame->format < 0)
  159. return AVERROR(EINVAL);
  160. if (frame->width > 0 && frame->height > 0)
  161. return get_video_buffer(frame, align);
  162. else if (frame->nb_samples > 0 && frame->channel_layout)
  163. return get_audio_buffer(frame, align);
  164. return AVERROR(EINVAL);
  165. }
  166. int av_frame_ref(AVFrame *dst, const AVFrame *src)
  167. {
  168. int i, ret = 0;
  169. dst->format = src->format;
  170. dst->width = src->width;
  171. dst->height = src->height;
  172. dst->channel_layout = src->channel_layout;
  173. dst->nb_samples = src->nb_samples;
  174. ret = av_frame_copy_props(dst, src);
  175. if (ret < 0)
  176. return ret;
  177. /* duplicate the frame data if it's not refcounted */
  178. if (!src->buf[0]) {
  179. ret = av_frame_get_buffer(dst, 32);
  180. if (ret < 0)
  181. return ret;
  182. ret = av_frame_copy(dst, src);
  183. if (ret < 0)
  184. av_frame_unref(dst);
  185. return ret;
  186. }
  187. /* ref the buffers */
  188. for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {
  189. dst->buf[i] = av_buffer_ref(src->buf[i]);
  190. if (!dst->buf[i]) {
  191. ret = AVERROR(ENOMEM);
  192. goto fail;
  193. }
  194. }
  195. if (src->extended_buf) {
  196. dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
  197. src->nb_extended_buf);
  198. if (!dst->extended_buf) {
  199. ret = AVERROR(ENOMEM);
  200. goto fail;
  201. }
  202. dst->nb_extended_buf = src->nb_extended_buf;
  203. for (i = 0; i < src->nb_extended_buf; i++) {
  204. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  205. if (!dst->extended_buf[i]) {
  206. ret = AVERROR(ENOMEM);
  207. goto fail;
  208. }
  209. }
  210. }
  211. /* duplicate extended data */
  212. if (src->extended_data != src->data) {
  213. int ch = av_get_channel_layout_nb_channels(src->channel_layout);
  214. if (!ch) {
  215. ret = AVERROR(EINVAL);
  216. goto fail;
  217. }
  218. dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
  219. if (!dst->extended_data) {
  220. ret = AVERROR(ENOMEM);
  221. goto fail;
  222. }
  223. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  224. } else
  225. dst->extended_data = dst->data;
  226. memcpy(dst->data, src->data, sizeof(src->data));
  227. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  228. return 0;
  229. fail:
  230. av_frame_unref(dst);
  231. return ret;
  232. }
  233. AVFrame *av_frame_clone(const AVFrame *src)
  234. {
  235. AVFrame *ret = av_frame_alloc();
  236. if (!ret)
  237. return NULL;
  238. if (av_frame_ref(ret, src) < 0)
  239. av_frame_free(&ret);
  240. return ret;
  241. }
  242. void av_frame_unref(AVFrame *frame)
  243. {
  244. int i;
  245. wipe_side_data(frame);
  246. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  247. av_buffer_unref(&frame->buf[i]);
  248. for (i = 0; i < frame->nb_extended_buf; i++)
  249. av_buffer_unref(&frame->extended_buf[i]);
  250. av_freep(&frame->extended_buf);
  251. get_frame_defaults(frame);
  252. }
  253. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  254. {
  255. *dst = *src;
  256. if (src->extended_data == src->data)
  257. dst->extended_data = dst->data;
  258. memset(src, 0, sizeof(*src));
  259. get_frame_defaults(src);
  260. }
  261. int av_frame_is_writable(AVFrame *frame)
  262. {
  263. int i, ret = 1;
  264. /* assume non-refcounted frames are not writable */
  265. if (!frame->buf[0])
  266. return 0;
  267. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
  268. ret &= !!av_buffer_is_writable(frame->buf[i]);
  269. for (i = 0; i < frame->nb_extended_buf; i++)
  270. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  271. return ret;
  272. }
  273. int av_frame_make_writable(AVFrame *frame)
  274. {
  275. AVFrame tmp;
  276. int ret;
  277. if (!frame->buf[0])
  278. return AVERROR(EINVAL);
  279. if (av_frame_is_writable(frame))
  280. return 0;
  281. memset(&tmp, 0, sizeof(tmp));
  282. tmp.format = frame->format;
  283. tmp.width = frame->width;
  284. tmp.height = frame->height;
  285. tmp.channel_layout = frame->channel_layout;
  286. tmp.nb_samples = frame->nb_samples;
  287. ret = av_frame_get_buffer(&tmp, 32);
  288. if (ret < 0)
  289. return ret;
  290. ret = av_frame_copy(&tmp, frame);
  291. if (ret < 0) {
  292. av_frame_unref(&tmp);
  293. return ret;
  294. }
  295. ret = av_frame_copy_props(&tmp, frame);
  296. if (ret < 0) {
  297. av_frame_unref(&tmp);
  298. return ret;
  299. }
  300. av_frame_unref(frame);
  301. *frame = tmp;
  302. if (tmp.data == tmp.extended_data)
  303. frame->extended_data = frame->data;
  304. return 0;
  305. }
  306. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  307. {
  308. int i;
  309. dst->key_frame = src->key_frame;
  310. dst->pict_type = src->pict_type;
  311. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  312. dst->pts = src->pts;
  313. dst->repeat_pict = src->repeat_pict;
  314. dst->interlaced_frame = src->interlaced_frame;
  315. dst->top_field_first = src->top_field_first;
  316. dst->palette_has_changed = src->palette_has_changed;
  317. dst->sample_rate = src->sample_rate;
  318. dst->opaque = src->opaque;
  319. dst->pkt_pts = src->pkt_pts;
  320. dst->pkt_dts = src->pkt_dts;
  321. dst->reordered_opaque = src->reordered_opaque;
  322. dst->quality = src->quality;
  323. dst->coded_picture_number = src->coded_picture_number;
  324. dst->display_picture_number = src->display_picture_number;
  325. dst->flags = src->flags;
  326. dst->color_primaries = src->color_primaries;
  327. dst->color_trc = src->color_trc;
  328. dst->colorspace = src->colorspace;
  329. dst->color_range = src->color_range;
  330. dst->chroma_location = src->chroma_location;
  331. memcpy(dst->error, src->error, sizeof(dst->error));
  332. for (i = 0; i < src->nb_side_data; i++) {
  333. const AVFrameSideData *sd_src = src->side_data[i];
  334. AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
  335. sd_src->size);
  336. if (!sd_dst) {
  337. wipe_side_data(dst);
  338. return AVERROR(ENOMEM);
  339. }
  340. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  341. av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
  342. }
  343. return 0;
  344. }
  345. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  346. {
  347. uint8_t *data;
  348. int planes, i;
  349. if (frame->nb_samples) {
  350. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  351. if (!channels)
  352. return NULL;
  353. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  354. } else
  355. planes = 4;
  356. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  357. return NULL;
  358. data = frame->extended_data[plane];
  359. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  360. AVBufferRef *buf = frame->buf[i];
  361. if (data >= buf->data && data < buf->data + buf->size)
  362. return buf;
  363. }
  364. for (i = 0; i < frame->nb_extended_buf; i++) {
  365. AVBufferRef *buf = frame->extended_buf[i];
  366. if (data >= buf->data && data < buf->data + buf->size)
  367. return buf;
  368. }
  369. return NULL;
  370. }
  371. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  372. enum AVFrameSideDataType type,
  373. int size)
  374. {
  375. AVFrameSideData *ret, **tmp;
  376. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  377. return NULL;
  378. tmp = av_realloc(frame->side_data,
  379. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  380. if (!tmp)
  381. return NULL;
  382. frame->side_data = tmp;
  383. ret = av_mallocz(sizeof(*ret));
  384. if (!ret)
  385. return NULL;
  386. ret->data = av_malloc(size);
  387. if (!ret->data) {
  388. av_freep(&ret);
  389. return NULL;
  390. }
  391. ret->size = size;
  392. ret->type = type;
  393. frame->side_data[frame->nb_side_data++] = ret;
  394. return ret;
  395. }
  396. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  397. enum AVFrameSideDataType type)
  398. {
  399. int i;
  400. for (i = 0; i < frame->nb_side_data; i++) {
  401. if (frame->side_data[i]->type == type)
  402. return frame->side_data[i];
  403. }
  404. return NULL;
  405. }
  406. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  407. {
  408. const uint8_t *src_data[4];
  409. int i, planes;
  410. if (dst->width != src->width ||
  411. dst->height != src->height)
  412. return AVERROR(EINVAL);
  413. planes = av_pix_fmt_count_planes(dst->format);
  414. for (i = 0; i < planes; i++)
  415. if (!dst->data[i] || !src->data[i])
  416. return AVERROR(EINVAL);
  417. memcpy(src_data, src->data, sizeof(src_data));
  418. av_image_copy(dst->data, dst->linesize,
  419. src_data, src->linesize,
  420. dst->format, dst->width, dst->height);
  421. return 0;
  422. }
  423. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  424. {
  425. int planar = av_sample_fmt_is_planar(dst->format);
  426. int channels = av_get_channel_layout_nb_channels(dst->channel_layout);
  427. int planes = planar ? channels : 1;
  428. int i;
  429. if (dst->nb_samples != src->nb_samples ||
  430. dst->channel_layout != src->channel_layout)
  431. return AVERROR(EINVAL);
  432. for (i = 0; i < planes; i++)
  433. if (!dst->extended_data[i] || !src->extended_data[i])
  434. return AVERROR(EINVAL);
  435. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  436. dst->nb_samples, channels, dst->format);
  437. return 0;
  438. }
  439. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  440. {
  441. if (dst->format != src->format || dst->format < 0)
  442. return AVERROR(EINVAL);
  443. if (dst->width > 0 && dst->height > 0)
  444. return frame_copy_video(dst, src);
  445. else if (dst->nb_samples > 0 && dst->channel_layout)
  446. return frame_copy_audio(dst, src);
  447. return AVERROR(EINVAL);
  448. }
  449. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  450. {
  451. int i;
  452. for (i = 0; i < frame->nb_side_data; i++) {
  453. AVFrameSideData *sd = frame->side_data[i];
  454. if (sd->type == type) {
  455. free_side_data(&frame->side_data[i]);
  456. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  457. frame->nb_side_data--;
  458. }
  459. }
  460. }