frame.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "channel_layout.h"
  19. #include "avassert.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. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  28. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  29. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  30. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  31. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  32. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  33. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  34. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  35. MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
  36. MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
  37. MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
  38. #define CHECK_CHANNELS_CONSISTENCY(frame) \
  39. av_assert2(!(frame)->channel_layout || \
  40. (frame)->channels == \
  41. av_get_channel_layout_nb_channels((frame)->channel_layout))
  42. AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
  43. #if FF_API_FRAME_QP
  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. FF_DISABLE_DEPRECATION_WARNINGS
  49. f->qscale_table = buf->data;
  50. f->qstride = stride;
  51. f->qscale_type = qp_type;
  52. FF_ENABLE_DEPRECATION_WARNINGS
  53. return 0;
  54. }
  55. int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
  56. {
  57. FF_DISABLE_DEPRECATION_WARNINGS
  58. *stride = f->qstride;
  59. *type = f->qscale_type;
  60. FF_ENABLE_DEPRECATION_WARNINGS
  61. if (!f->qp_table_buf)
  62. return NULL;
  63. return f->qp_table_buf->data;
  64. }
  65. #endif
  66. const char *av_get_colorspace_name(enum AVColorSpace val)
  67. {
  68. static const char * const name[] = {
  69. [AVCOL_SPC_RGB] = "GBR",
  70. [AVCOL_SPC_BT709] = "bt709",
  71. [AVCOL_SPC_FCC] = "fcc",
  72. [AVCOL_SPC_BT470BG] = "bt470bg",
  73. [AVCOL_SPC_SMPTE170M] = "smpte170m",
  74. [AVCOL_SPC_SMPTE240M] = "smpte240m",
  75. [AVCOL_SPC_YCOCG] = "YCgCo",
  76. };
  77. if ((unsigned)val >= FF_ARRAY_ELEMS(name))
  78. return NULL;
  79. return name[val];
  80. }
  81. static void get_frame_defaults(AVFrame *frame)
  82. {
  83. if (frame->extended_data != frame->data)
  84. av_freep(&frame->extended_data);
  85. memset(frame, 0, sizeof(*frame));
  86. frame->pts =
  87. frame->pkt_dts = AV_NOPTS_VALUE;
  88. #if FF_API_PKT_PTS
  89. FF_DISABLE_DEPRECATION_WARNINGS
  90. frame->pkt_pts = AV_NOPTS_VALUE;
  91. FF_ENABLE_DEPRECATION_WARNINGS
  92. #endif
  93. frame->best_effort_timestamp = AV_NOPTS_VALUE;
  94. frame->pkt_duration = 0;
  95. frame->pkt_pos = -1;
  96. frame->pkt_size = -1;
  97. frame->key_frame = 1;
  98. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  99. frame->format = -1; /* unknown */
  100. frame->extended_data = frame->data;
  101. frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
  102. frame->color_trc = AVCOL_TRC_UNSPECIFIED;
  103. frame->colorspace = AVCOL_SPC_UNSPECIFIED;
  104. frame->color_range = AVCOL_RANGE_UNSPECIFIED;
  105. frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  106. frame->flags = 0;
  107. }
  108. static void free_side_data(AVFrameSideData **ptr_sd)
  109. {
  110. AVFrameSideData *sd = *ptr_sd;
  111. av_buffer_unref(&sd->buf);
  112. av_dict_free(&sd->metadata);
  113. av_freep(ptr_sd);
  114. }
  115. static void wipe_side_data(AVFrame *frame)
  116. {
  117. int i;
  118. for (i = 0; i < frame->nb_side_data; i++) {
  119. free_side_data(&frame->side_data[i]);
  120. }
  121. frame->nb_side_data = 0;
  122. av_freep(&frame->side_data);
  123. }
  124. AVFrame *av_frame_alloc(void)
  125. {
  126. AVFrame *frame = av_mallocz(sizeof(*frame));
  127. if (!frame)
  128. return NULL;
  129. frame->extended_data = NULL;
  130. get_frame_defaults(frame);
  131. return frame;
  132. }
  133. void av_frame_free(AVFrame **frame)
  134. {
  135. if (!frame || !*frame)
  136. return;
  137. av_frame_unref(*frame);
  138. av_freep(frame);
  139. }
  140. static int get_video_buffer(AVFrame *frame, int align)
  141. {
  142. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  143. int ret, i;
  144. if (!desc)
  145. return AVERROR(EINVAL);
  146. if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
  147. return ret;
  148. if (!frame->linesize[0]) {
  149. for(i=1; i<=align; i+=i) {
  150. ret = av_image_fill_linesizes(frame->linesize, frame->format,
  151. FFALIGN(frame->width, i));
  152. if (ret < 0)
  153. return ret;
  154. if (!(frame->linesize[0] & (align-1)))
  155. break;
  156. }
  157. for (i = 0; i < 4 && frame->linesize[i]; i++)
  158. frame->linesize[i] = FFALIGN(frame->linesize[i], align);
  159. }
  160. for (i = 0; i < 4 && frame->linesize[i]; i++) {
  161. int h = FFALIGN(frame->height, 32);
  162. if (i == 1 || i == 2)
  163. h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
  164. frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
  165. if (!frame->buf[i])
  166. goto fail;
  167. frame->data[i] = frame->buf[i]->data;
  168. }
  169. if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  170. av_buffer_unref(&frame->buf[1]);
  171. frame->buf[1] = av_buffer_alloc(AVPALETTE_SIZE);
  172. if (!frame->buf[1])
  173. goto fail;
  174. frame->data[1] = frame->buf[1]->data;
  175. }
  176. frame->extended_data = frame->data;
  177. return 0;
  178. fail:
  179. av_frame_unref(frame);
  180. return AVERROR(ENOMEM);
  181. }
  182. static int get_audio_buffer(AVFrame *frame, int align)
  183. {
  184. int channels;
  185. int planar = av_sample_fmt_is_planar(frame->format);
  186. int planes;
  187. int ret, i;
  188. if (!frame->channels)
  189. frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  190. channels = frame->channels;
  191. planes = planar ? channels : 1;
  192. CHECK_CHANNELS_CONSISTENCY(frame);
  193. if (!frame->linesize[0]) {
  194. ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
  195. frame->nb_samples, frame->format,
  196. align);
  197. if (ret < 0)
  198. return ret;
  199. }
  200. if (planes > AV_NUM_DATA_POINTERS) {
  201. frame->extended_data = av_mallocz_array(planes,
  202. sizeof(*frame->extended_data));
  203. frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
  204. sizeof(*frame->extended_buf));
  205. if (!frame->extended_data || !frame->extended_buf) {
  206. av_freep(&frame->extended_data);
  207. av_freep(&frame->extended_buf);
  208. return AVERROR(ENOMEM);
  209. }
  210. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  211. } else
  212. frame->extended_data = frame->data;
  213. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  214. frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
  215. if (!frame->buf[i]) {
  216. av_frame_unref(frame);
  217. return AVERROR(ENOMEM);
  218. }
  219. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  220. }
  221. for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
  222. frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
  223. if (!frame->extended_buf[i]) {
  224. av_frame_unref(frame);
  225. return AVERROR(ENOMEM);
  226. }
  227. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  228. }
  229. return 0;
  230. }
  231. int av_frame_get_buffer(AVFrame *frame, int align)
  232. {
  233. if (frame->format < 0)
  234. return AVERROR(EINVAL);
  235. if (frame->width > 0 && frame->height > 0)
  236. return get_video_buffer(frame, align);
  237. else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
  238. return get_audio_buffer(frame, align);
  239. return AVERROR(EINVAL);
  240. }
  241. static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
  242. {
  243. int i;
  244. dst->key_frame = src->key_frame;
  245. dst->pict_type = src->pict_type;
  246. dst->sample_aspect_ratio = src->sample_aspect_ratio;
  247. dst->pts = src->pts;
  248. dst->repeat_pict = src->repeat_pict;
  249. dst->interlaced_frame = src->interlaced_frame;
  250. dst->top_field_first = src->top_field_first;
  251. dst->palette_has_changed = src->palette_has_changed;
  252. dst->sample_rate = src->sample_rate;
  253. dst->opaque = src->opaque;
  254. #if FF_API_PKT_PTS
  255. FF_DISABLE_DEPRECATION_WARNINGS
  256. dst->pkt_pts = src->pkt_pts;
  257. FF_ENABLE_DEPRECATION_WARNINGS
  258. #endif
  259. dst->pkt_dts = src->pkt_dts;
  260. dst->pkt_pos = src->pkt_pos;
  261. dst->pkt_size = src->pkt_size;
  262. dst->pkt_duration = src->pkt_duration;
  263. dst->reordered_opaque = src->reordered_opaque;
  264. dst->quality = src->quality;
  265. dst->best_effort_timestamp = src->best_effort_timestamp;
  266. dst->coded_picture_number = src->coded_picture_number;
  267. dst->display_picture_number = src->display_picture_number;
  268. dst->flags = src->flags;
  269. dst->decode_error_flags = src->decode_error_flags;
  270. dst->color_primaries = src->color_primaries;
  271. dst->color_trc = src->color_trc;
  272. dst->colorspace = src->colorspace;
  273. dst->color_range = src->color_range;
  274. dst->chroma_location = src->chroma_location;
  275. av_dict_copy(&dst->metadata, src->metadata, 0);
  276. #if FF_API_ERROR_FRAME
  277. FF_DISABLE_DEPRECATION_WARNINGS
  278. memcpy(dst->error, src->error, sizeof(dst->error));
  279. FF_ENABLE_DEPRECATION_WARNINGS
  280. #endif
  281. for (i = 0; i < src->nb_side_data; i++) {
  282. const AVFrameSideData *sd_src = src->side_data[i];
  283. AVFrameSideData *sd_dst;
  284. if ( sd_src->type == AV_FRAME_DATA_PANSCAN
  285. && (src->width != dst->width || src->height != dst->height))
  286. continue;
  287. if (force_copy) {
  288. sd_dst = av_frame_new_side_data(dst, sd_src->type,
  289. sd_src->size);
  290. if (!sd_dst) {
  291. wipe_side_data(dst);
  292. return AVERROR(ENOMEM);
  293. }
  294. memcpy(sd_dst->data, sd_src->data, sd_src->size);
  295. } else {
  296. sd_dst = av_frame_new_side_data(dst, sd_src->type, 0);
  297. if (!sd_dst) {
  298. wipe_side_data(dst);
  299. return AVERROR(ENOMEM);
  300. }
  301. sd_dst->buf = av_buffer_ref(sd_src->buf);
  302. if (!sd_dst->buf) {
  303. wipe_side_data(dst);
  304. return AVERROR(ENOMEM);
  305. }
  306. sd_dst->data = sd_dst->buf->data;
  307. sd_dst->size = sd_dst->buf->size;
  308. }
  309. av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
  310. }
  311. #if FF_API_FRAME_QP
  312. FF_DISABLE_DEPRECATION_WARNINGS
  313. dst->qscale_table = NULL;
  314. dst->qstride = 0;
  315. dst->qscale_type = 0;
  316. av_buffer_unref(&dst->qp_table_buf);
  317. if (src->qp_table_buf) {
  318. dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
  319. if (dst->qp_table_buf) {
  320. dst->qscale_table = dst->qp_table_buf->data;
  321. dst->qstride = src->qstride;
  322. dst->qscale_type = src->qscale_type;
  323. }
  324. }
  325. FF_ENABLE_DEPRECATION_WARNINGS
  326. #endif
  327. return 0;
  328. }
  329. int av_frame_ref(AVFrame *dst, const AVFrame *src)
  330. {
  331. int i, ret = 0;
  332. av_assert1(dst->width == 0 && dst->height == 0);
  333. av_assert1(dst->channels == 0);
  334. dst->format = src->format;
  335. dst->width = src->width;
  336. dst->height = src->height;
  337. dst->channels = src->channels;
  338. dst->channel_layout = src->channel_layout;
  339. dst->nb_samples = src->nb_samples;
  340. ret = frame_copy_props(dst, src, 0);
  341. if (ret < 0)
  342. return ret;
  343. /* duplicate the frame data if it's not refcounted */
  344. if (!src->buf[0]) {
  345. ret = av_frame_get_buffer(dst, 32);
  346. if (ret < 0)
  347. return ret;
  348. ret = av_frame_copy(dst, src);
  349. if (ret < 0)
  350. av_frame_unref(dst);
  351. return ret;
  352. }
  353. /* ref the buffers */
  354. for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
  355. if (!src->buf[i])
  356. continue;
  357. dst->buf[i] = av_buffer_ref(src->buf[i]);
  358. if (!dst->buf[i]) {
  359. ret = AVERROR(ENOMEM);
  360. goto fail;
  361. }
  362. }
  363. if (src->extended_buf) {
  364. dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
  365. src->nb_extended_buf);
  366. if (!dst->extended_buf) {
  367. ret = AVERROR(ENOMEM);
  368. goto fail;
  369. }
  370. dst->nb_extended_buf = src->nb_extended_buf;
  371. for (i = 0; i < src->nb_extended_buf; i++) {
  372. dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
  373. if (!dst->extended_buf[i]) {
  374. ret = AVERROR(ENOMEM);
  375. goto fail;
  376. }
  377. }
  378. }
  379. if (src->hw_frames_ctx) {
  380. dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
  381. if (!dst->hw_frames_ctx) {
  382. ret = AVERROR(ENOMEM);
  383. goto fail;
  384. }
  385. }
  386. /* duplicate extended data */
  387. if (src->extended_data != src->data) {
  388. int ch = src->channels;
  389. if (!ch) {
  390. ret = AVERROR(EINVAL);
  391. goto fail;
  392. }
  393. CHECK_CHANNELS_CONSISTENCY(src);
  394. dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
  395. if (!dst->extended_data) {
  396. ret = AVERROR(ENOMEM);
  397. goto fail;
  398. }
  399. memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
  400. } else
  401. dst->extended_data = dst->data;
  402. memcpy(dst->data, src->data, sizeof(src->data));
  403. memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
  404. return 0;
  405. fail:
  406. av_frame_unref(dst);
  407. return ret;
  408. }
  409. AVFrame *av_frame_clone(const AVFrame *src)
  410. {
  411. AVFrame *ret = av_frame_alloc();
  412. if (!ret)
  413. return NULL;
  414. if (av_frame_ref(ret, src) < 0)
  415. av_frame_free(&ret);
  416. return ret;
  417. }
  418. void av_frame_unref(AVFrame *frame)
  419. {
  420. int i;
  421. if (!frame)
  422. return;
  423. wipe_side_data(frame);
  424. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  425. av_buffer_unref(&frame->buf[i]);
  426. for (i = 0; i < frame->nb_extended_buf; i++)
  427. av_buffer_unref(&frame->extended_buf[i]);
  428. av_freep(&frame->extended_buf);
  429. av_dict_free(&frame->metadata);
  430. #if FF_API_FRAME_QP
  431. av_buffer_unref(&frame->qp_table_buf);
  432. #endif
  433. av_buffer_unref(&frame->hw_frames_ctx);
  434. get_frame_defaults(frame);
  435. }
  436. void av_frame_move_ref(AVFrame *dst, AVFrame *src)
  437. {
  438. av_assert1(dst->width == 0 && dst->height == 0);
  439. av_assert1(dst->channels == 0);
  440. *dst = *src;
  441. if (src->extended_data == src->data)
  442. dst->extended_data = dst->data;
  443. memset(src, 0, sizeof(*src));
  444. get_frame_defaults(src);
  445. }
  446. int av_frame_is_writable(AVFrame *frame)
  447. {
  448. int i, ret = 1;
  449. /* assume non-refcounted frames are not writable */
  450. if (!frame->buf[0])
  451. return 0;
  452. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
  453. if (frame->buf[i])
  454. ret &= !!av_buffer_is_writable(frame->buf[i]);
  455. for (i = 0; i < frame->nb_extended_buf; i++)
  456. ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
  457. return ret;
  458. }
  459. int av_frame_make_writable(AVFrame *frame)
  460. {
  461. AVFrame tmp;
  462. int ret;
  463. if (!frame->buf[0])
  464. return AVERROR(EINVAL);
  465. if (av_frame_is_writable(frame))
  466. return 0;
  467. memset(&tmp, 0, sizeof(tmp));
  468. tmp.format = frame->format;
  469. tmp.width = frame->width;
  470. tmp.height = frame->height;
  471. tmp.channels = frame->channels;
  472. tmp.channel_layout = frame->channel_layout;
  473. tmp.nb_samples = frame->nb_samples;
  474. ret = av_frame_get_buffer(&tmp, 32);
  475. if (ret < 0)
  476. return ret;
  477. ret = av_frame_copy(&tmp, frame);
  478. if (ret < 0) {
  479. av_frame_unref(&tmp);
  480. return ret;
  481. }
  482. ret = av_frame_copy_props(&tmp, frame);
  483. if (ret < 0) {
  484. av_frame_unref(&tmp);
  485. return ret;
  486. }
  487. av_frame_unref(frame);
  488. *frame = tmp;
  489. if (tmp.data == tmp.extended_data)
  490. frame->extended_data = frame->data;
  491. return 0;
  492. }
  493. int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
  494. {
  495. return frame_copy_props(dst, src, 1);
  496. }
  497. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
  498. {
  499. uint8_t *data;
  500. int planes, i;
  501. if (frame->nb_samples) {
  502. int channels = frame->channels;
  503. if (!channels)
  504. return NULL;
  505. CHECK_CHANNELS_CONSISTENCY(frame);
  506. planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
  507. } else
  508. planes = 4;
  509. if (plane < 0 || plane >= planes || !frame->extended_data[plane])
  510. return NULL;
  511. data = frame->extended_data[plane];
  512. for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
  513. AVBufferRef *buf = frame->buf[i];
  514. if (data >= buf->data && data < buf->data + buf->size)
  515. return buf;
  516. }
  517. for (i = 0; i < frame->nb_extended_buf; i++) {
  518. AVBufferRef *buf = frame->extended_buf[i];
  519. if (data >= buf->data && data < buf->data + buf->size)
  520. return buf;
  521. }
  522. return NULL;
  523. }
  524. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  525. enum AVFrameSideDataType type,
  526. int size)
  527. {
  528. AVFrameSideData *ret, **tmp;
  529. if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
  530. return NULL;
  531. tmp = av_realloc(frame->side_data,
  532. (frame->nb_side_data + 1) * sizeof(*frame->side_data));
  533. if (!tmp)
  534. return NULL;
  535. frame->side_data = tmp;
  536. ret = av_mallocz(sizeof(*ret));
  537. if (!ret)
  538. return NULL;
  539. if (size > 0) {
  540. ret->buf = av_buffer_alloc(size);
  541. if (!ret->buf) {
  542. av_freep(&ret);
  543. return NULL;
  544. }
  545. ret->data = ret->buf->data;
  546. ret->size = size;
  547. }
  548. ret->type = type;
  549. frame->side_data[frame->nb_side_data++] = ret;
  550. return ret;
  551. }
  552. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  553. enum AVFrameSideDataType type)
  554. {
  555. int i;
  556. for (i = 0; i < frame->nb_side_data; i++) {
  557. if (frame->side_data[i]->type == type)
  558. return frame->side_data[i];
  559. }
  560. return NULL;
  561. }
  562. static int frame_copy_video(AVFrame *dst, const AVFrame *src)
  563. {
  564. const uint8_t *src_data[4];
  565. int i, planes;
  566. if (dst->width < src->width ||
  567. dst->height < src->height)
  568. return AVERROR(EINVAL);
  569. planes = av_pix_fmt_count_planes(dst->format);
  570. for (i = 0; i < planes; i++)
  571. if (!dst->data[i] || !src->data[i])
  572. return AVERROR(EINVAL);
  573. memcpy(src_data, src->data, sizeof(src_data));
  574. av_image_copy(dst->data, dst->linesize,
  575. src_data, src->linesize,
  576. dst->format, src->width, src->height);
  577. return 0;
  578. }
  579. static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
  580. {
  581. int planar = av_sample_fmt_is_planar(dst->format);
  582. int channels = dst->channels;
  583. int planes = planar ? channels : 1;
  584. int i;
  585. if (dst->nb_samples != src->nb_samples ||
  586. dst->channels != src->channels ||
  587. dst->channel_layout != src->channel_layout)
  588. return AVERROR(EINVAL);
  589. CHECK_CHANNELS_CONSISTENCY(src);
  590. for (i = 0; i < planes; i++)
  591. if (!dst->extended_data[i] || !src->extended_data[i])
  592. return AVERROR(EINVAL);
  593. av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
  594. dst->nb_samples, channels, dst->format);
  595. return 0;
  596. }
  597. int av_frame_copy(AVFrame *dst, const AVFrame *src)
  598. {
  599. if (dst->format != src->format || dst->format < 0)
  600. return AVERROR(EINVAL);
  601. if (dst->width > 0 && dst->height > 0)
  602. return frame_copy_video(dst, src);
  603. else if (dst->nb_samples > 0 && dst->channel_layout)
  604. return frame_copy_audio(dst, src);
  605. return AVERROR(EINVAL);
  606. }
  607. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
  608. {
  609. int i;
  610. for (i = 0; i < frame->nb_side_data; i++) {
  611. AVFrameSideData *sd = frame->side_data[i];
  612. if (sd->type == type) {
  613. free_side_data(&frame->side_data[i]);
  614. frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
  615. frame->nb_side_data--;
  616. }
  617. }
  618. }
  619. const char *av_frame_side_data_name(enum AVFrameSideDataType type)
  620. {
  621. switch(type) {
  622. case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
  623. case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
  624. case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
  625. case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
  626. case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
  627. case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
  628. case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
  629. case AV_FRAME_DATA_AFD: return "Active format description";
  630. case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
  631. case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
  632. case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
  633. case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
  634. case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";
  635. }
  636. return NULL;
  637. }