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.

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