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.

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