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.

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