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.

672 lines
19KB

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