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.

1837 lines
56KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "libavutil/dict.h"
  35. #include "avcodec.h"
  36. #include "dsputil.h"
  37. #include "libavutil/opt.h"
  38. #include "imgconvert.h"
  39. #include "thread.h"
  40. #include "audioconvert.h"
  41. #include "internal.h"
  42. #include "bytestream.h"
  43. #include <stdlib.h>
  44. #include <stdarg.h>
  45. #include <limits.h>
  46. #include <float.h>
  47. static int volatile entangled_thread_counter=0;
  48. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  49. static void *codec_mutex;
  50. static void *avformat_mutex;
  51. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  52. {
  53. if(min_size < *size)
  54. return ptr;
  55. min_size= FFMAX(17*min_size/16 + 32, min_size);
  56. ptr= av_realloc(ptr, min_size);
  57. if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
  58. min_size= 0;
  59. *size= min_size;
  60. return ptr;
  61. }
  62. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  63. {
  64. void **p = ptr;
  65. if (min_size < *size)
  66. return;
  67. min_size= FFMAX(17*min_size/16 + 32, min_size);
  68. av_free(*p);
  69. *p = av_malloc(min_size);
  70. if (!*p) min_size = 0;
  71. *size= min_size;
  72. }
  73. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  74. {
  75. void **p = ptr;
  76. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  77. av_freep(p);
  78. *size = 0;
  79. return;
  80. }
  81. av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  82. if (*size)
  83. memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  84. }
  85. /* encoder management */
  86. static AVCodec *first_avcodec = NULL;
  87. AVCodec *av_codec_next(AVCodec *c){
  88. if(c) return c->next;
  89. else return first_avcodec;
  90. }
  91. static void avcodec_init(void)
  92. {
  93. static int initialized = 0;
  94. if (initialized != 0)
  95. return;
  96. initialized = 1;
  97. dsputil_static_init();
  98. }
  99. static av_always_inline int codec_is_encoder(AVCodec *codec)
  100. {
  101. return codec && (codec->encode || codec->encode2);
  102. }
  103. static av_always_inline int codec_is_decoder(AVCodec *codec)
  104. {
  105. return codec && codec->decode;
  106. }
  107. void avcodec_register(AVCodec *codec)
  108. {
  109. AVCodec **p;
  110. avcodec_init();
  111. p = &first_avcodec;
  112. while (*p != NULL) p = &(*p)->next;
  113. *p = codec;
  114. codec->next = NULL;
  115. if (codec->init_static_data)
  116. codec->init_static_data(codec);
  117. }
  118. unsigned avcodec_get_edge_width(void)
  119. {
  120. return EDGE_WIDTH;
  121. }
  122. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  123. s->coded_width = width;
  124. s->coded_height= height;
  125. s->width = -((-width )>>s->lowres);
  126. s->height= -((-height)>>s->lowres);
  127. }
  128. #define INTERNAL_BUFFER_SIZE (32+1)
  129. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  130. int linesize_align[AV_NUM_DATA_POINTERS])
  131. {
  132. int i;
  133. int w_align= 1;
  134. int h_align= 1;
  135. switch(s->pix_fmt){
  136. case PIX_FMT_YUV420P:
  137. case PIX_FMT_YUYV422:
  138. case PIX_FMT_UYVY422:
  139. case PIX_FMT_YUV422P:
  140. case PIX_FMT_YUV440P:
  141. case PIX_FMT_YUV444P:
  142. case PIX_FMT_GBRP:
  143. case PIX_FMT_GRAY8:
  144. case PIX_FMT_GRAY16BE:
  145. case PIX_FMT_GRAY16LE:
  146. case PIX_FMT_YUVJ420P:
  147. case PIX_FMT_YUVJ422P:
  148. case PIX_FMT_YUVJ440P:
  149. case PIX_FMT_YUVJ444P:
  150. case PIX_FMT_YUVA420P:
  151. case PIX_FMT_YUV420P9LE:
  152. case PIX_FMT_YUV420P9BE:
  153. case PIX_FMT_YUV420P10LE:
  154. case PIX_FMT_YUV420P10BE:
  155. case PIX_FMT_YUV422P9LE:
  156. case PIX_FMT_YUV422P9BE:
  157. case PIX_FMT_YUV422P10LE:
  158. case PIX_FMT_YUV422P10BE:
  159. case PIX_FMT_YUV444P9LE:
  160. case PIX_FMT_YUV444P9BE:
  161. case PIX_FMT_YUV444P10LE:
  162. case PIX_FMT_YUV444P10BE:
  163. case PIX_FMT_GBRP9LE:
  164. case PIX_FMT_GBRP9BE:
  165. case PIX_FMT_GBRP10LE:
  166. case PIX_FMT_GBRP10BE:
  167. w_align = 16; //FIXME assume 16 pixel per macroblock
  168. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  169. break;
  170. case PIX_FMT_YUV411P:
  171. case PIX_FMT_UYYVYY411:
  172. w_align=32;
  173. h_align=8;
  174. break;
  175. case PIX_FMT_YUV410P:
  176. if(s->codec_id == CODEC_ID_SVQ1){
  177. w_align=64;
  178. h_align=64;
  179. }
  180. case PIX_FMT_RGB555:
  181. if(s->codec_id == CODEC_ID_RPZA){
  182. w_align=4;
  183. h_align=4;
  184. }
  185. case PIX_FMT_PAL8:
  186. case PIX_FMT_BGR8:
  187. case PIX_FMT_RGB8:
  188. if(s->codec_id == CODEC_ID_SMC){
  189. w_align=4;
  190. h_align=4;
  191. }
  192. break;
  193. case PIX_FMT_BGR24:
  194. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  195. w_align=4;
  196. h_align=4;
  197. }
  198. break;
  199. default:
  200. w_align= 1;
  201. h_align= 1;
  202. break;
  203. }
  204. *width = FFALIGN(*width , w_align);
  205. *height= FFALIGN(*height, h_align);
  206. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  207. *height+=2; // some of the optimized chroma MC reads one line too much
  208. // which is also done in mpeg decoders with lowres > 0
  209. for (i = 0; i < 4; i++)
  210. linesize_align[i] = STRIDE_ALIGN;
  211. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  212. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  213. //picture size unneccessarily in some cases. The solution here is not
  214. //pretty and better ideas are welcome!
  215. #if HAVE_MMX
  216. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  217. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  218. s->codec_id == CODEC_ID_VP6A) {
  219. for (i = 0; i < 4; i++)
  220. linesize_align[i] = 16;
  221. }
  222. #endif
  223. }
  224. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  225. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  226. int linesize_align[AV_NUM_DATA_POINTERS];
  227. int align;
  228. avcodec_align_dimensions2(s, width, height, linesize_align);
  229. align = FFMAX(linesize_align[0], linesize_align[3]);
  230. linesize_align[1] <<= chroma_shift;
  231. linesize_align[2] <<= chroma_shift;
  232. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  233. *width=FFALIGN(*width, align);
  234. }
  235. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  236. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  237. int buf_size, int align)
  238. {
  239. int ch, planar, needed_size, ret = 0;
  240. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  241. frame->nb_samples, sample_fmt,
  242. align);
  243. if (buf_size < needed_size)
  244. return AVERROR(EINVAL);
  245. planar = av_sample_fmt_is_planar(sample_fmt);
  246. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  247. if (!(frame->extended_data = av_mallocz(nb_channels *
  248. sizeof(*frame->extended_data))))
  249. return AVERROR(ENOMEM);
  250. } else {
  251. frame->extended_data = frame->data;
  252. }
  253. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  254. buf, nb_channels, frame->nb_samples,
  255. sample_fmt, align)) < 0) {
  256. if (frame->extended_data != frame->data)
  257. av_free(frame->extended_data);
  258. return ret;
  259. }
  260. if (frame->extended_data != frame->data) {
  261. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  262. frame->data[ch] = frame->extended_data[ch];
  263. }
  264. return ret;
  265. }
  266. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  267. {
  268. AVCodecInternal *avci = avctx->internal;
  269. InternalBuffer *buf;
  270. int buf_size, ret;
  271. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  272. frame->nb_samples, avctx->sample_fmt,
  273. 32);
  274. if (buf_size < 0)
  275. return AVERROR(EINVAL);
  276. /* allocate InternalBuffer if needed */
  277. if (!avci->buffer) {
  278. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  279. if (!avci->buffer)
  280. return AVERROR(ENOMEM);
  281. }
  282. buf = avci->buffer;
  283. /* if there is a previously-used internal buffer, check its size and
  284. channel count to see if we can reuse it */
  285. if (buf->extended_data) {
  286. /* if current buffer is too small, free it */
  287. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  288. av_free(buf->extended_data[0]);
  289. if (buf->extended_data != buf->data)
  290. av_free(&buf->extended_data);
  291. buf->extended_data = NULL;
  292. buf->data[0] = NULL;
  293. }
  294. /* if number of channels has changed, reset and/or free extended data
  295. pointers but leave data buffer in buf->data[0] for reuse */
  296. if (buf->nb_channels != avctx->channels) {
  297. if (buf->extended_data != buf->data)
  298. av_free(buf->extended_data);
  299. buf->extended_data = NULL;
  300. }
  301. }
  302. /* if there is no previous buffer or the previous buffer cannot be used
  303. as-is, allocate a new buffer and/or rearrange the channel pointers */
  304. if (!buf->extended_data) {
  305. if (!buf->data[0]) {
  306. if (!(buf->data[0] = av_mallocz(buf_size)))
  307. return AVERROR(ENOMEM);
  308. buf->audio_data_size = buf_size;
  309. }
  310. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  311. avctx->sample_fmt, buf->data[0],
  312. buf->audio_data_size, 32)))
  313. return ret;
  314. if (frame->extended_data == frame->data)
  315. buf->extended_data = buf->data;
  316. else
  317. buf->extended_data = frame->extended_data;
  318. memcpy(buf->data, frame->data, sizeof(frame->data));
  319. buf->linesize[0] = frame->linesize[0];
  320. buf->nb_channels = avctx->channels;
  321. } else {
  322. /* copy InternalBuffer info to the AVFrame */
  323. frame->extended_data = buf->extended_data;
  324. frame->linesize[0] = buf->linesize[0];
  325. memcpy(frame->data, buf->data, sizeof(frame->data));
  326. }
  327. frame->type = FF_BUFFER_TYPE_INTERNAL;
  328. if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
  329. else frame->pkt_pts = AV_NOPTS_VALUE;
  330. frame->reordered_opaque = avctx->reordered_opaque;
  331. if (avctx->debug & FF_DEBUG_BUFFERS)
  332. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  333. "internal audio buffer used\n", frame);
  334. return 0;
  335. }
  336. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  337. {
  338. int i;
  339. int w= s->width;
  340. int h= s->height;
  341. InternalBuffer *buf;
  342. AVCodecInternal *avci = s->internal;
  343. if(pic->data[0]!=NULL) {
  344. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  345. return -1;
  346. }
  347. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  348. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  349. return -1;
  350. }
  351. if(av_image_check_size(w, h, 0, s))
  352. return -1;
  353. if (!avci->buffer) {
  354. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  355. sizeof(InternalBuffer));
  356. }
  357. buf = &avci->buffer[avci->buffer_count];
  358. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  359. if(s->active_thread_type&FF_THREAD_FRAME) {
  360. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  361. return -1;
  362. }
  363. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  364. av_freep(&buf->base[i]);
  365. buf->data[i]= NULL;
  366. }
  367. }
  368. if (!buf->base[0]) {
  369. int h_chroma_shift, v_chroma_shift;
  370. int size[4] = {0};
  371. int tmpsize;
  372. int unaligned;
  373. AVPicture picture;
  374. int stride_align[AV_NUM_DATA_POINTERS];
  375. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  376. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  377. avcodec_align_dimensions2(s, &w, &h, stride_align);
  378. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  379. w+= EDGE_WIDTH*2;
  380. h+= EDGE_WIDTH*2;
  381. }
  382. do {
  383. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  384. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  385. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  386. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  387. w += w & ~(w-1);
  388. unaligned = 0;
  389. for (i=0; i<4; i++){
  390. unaligned |= picture.linesize[i] % stride_align[i];
  391. }
  392. } while (unaligned);
  393. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  394. if (tmpsize < 0)
  395. return -1;
  396. for (i=0; i<3 && picture.data[i+1]; i++)
  397. size[i] = picture.data[i+1] - picture.data[i];
  398. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  399. memset(buf->base, 0, sizeof(buf->base));
  400. memset(buf->data, 0, sizeof(buf->data));
  401. for(i=0; i<4 && size[i]; i++){
  402. const int h_shift= i==0 ? 0 : h_chroma_shift;
  403. const int v_shift= i==0 ? 0 : v_chroma_shift;
  404. buf->linesize[i]= picture.linesize[i];
  405. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  406. if(buf->base[i]==NULL) return -1;
  407. memset(buf->base[i], 128, size[i]);
  408. // no edge if EDGE EMU or not planar YUV
  409. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  410. buf->data[i] = buf->base[i];
  411. else
  412. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  413. }
  414. for (; i < AV_NUM_DATA_POINTERS; i++) {
  415. buf->base[i] = buf->data[i] = NULL;
  416. buf->linesize[i] = 0;
  417. }
  418. if(size[1] && !size[2])
  419. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  420. buf->width = s->width;
  421. buf->height = s->height;
  422. buf->pix_fmt= s->pix_fmt;
  423. }
  424. pic->type= FF_BUFFER_TYPE_INTERNAL;
  425. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  426. pic->base[i]= buf->base[i];
  427. pic->data[i]= buf->data[i];
  428. pic->linesize[i]= buf->linesize[i];
  429. }
  430. pic->extended_data = pic->data;
  431. avci->buffer_count++;
  432. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  433. else pic->pkt_pts= AV_NOPTS_VALUE;
  434. pic->reordered_opaque= s->reordered_opaque;
  435. if(s->debug&FF_DEBUG_BUFFERS)
  436. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  437. "buffers used\n", pic, avci->buffer_count);
  438. return 0;
  439. }
  440. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  441. {
  442. switch (avctx->codec_type) {
  443. case AVMEDIA_TYPE_VIDEO:
  444. return video_get_buffer(avctx, frame);
  445. case AVMEDIA_TYPE_AUDIO:
  446. return audio_get_buffer(avctx, frame);
  447. default:
  448. return -1;
  449. }
  450. }
  451. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  452. int i;
  453. InternalBuffer *buf, *last;
  454. AVCodecInternal *avci = s->internal;
  455. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  456. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  457. assert(avci->buffer_count);
  458. if (avci->buffer) {
  459. buf = NULL; /* avoids warning */
  460. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  461. buf = &avci->buffer[i];
  462. if (buf->data[0] == pic->data[0])
  463. break;
  464. }
  465. assert(i < avci->buffer_count);
  466. avci->buffer_count--;
  467. last = &avci->buffer[avci->buffer_count];
  468. if (buf != last)
  469. FFSWAP(InternalBuffer, *buf, *last);
  470. }
  471. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  472. pic->data[i]=NULL;
  473. // pic->base[i]=NULL;
  474. }
  475. //printf("R%X\n", pic->opaque);
  476. if(s->debug&FF_DEBUG_BUFFERS)
  477. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  478. "buffers used\n", pic, avci->buffer_count);
  479. }
  480. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  481. AVFrame temp_pic;
  482. int i;
  483. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  484. /* If no picture return a new buffer */
  485. if(pic->data[0] == NULL) {
  486. /* We will copy from buffer, so must be readable */
  487. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  488. return s->get_buffer(s, pic);
  489. }
  490. /* If internal buffer type return the same buffer */
  491. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  492. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  493. else pic->pkt_pts= AV_NOPTS_VALUE;
  494. pic->reordered_opaque= s->reordered_opaque;
  495. return 0;
  496. }
  497. /*
  498. * Not internal type and reget_buffer not overridden, emulate cr buffer
  499. */
  500. temp_pic = *pic;
  501. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  502. pic->data[i] = pic->base[i] = NULL;
  503. pic->opaque = NULL;
  504. /* Allocate new frame */
  505. if (s->get_buffer(s, pic))
  506. return -1;
  507. /* Copy image data from old buffer to new buffer */
  508. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  509. s->height);
  510. s->release_buffer(s, &temp_pic); // Release old frame
  511. return 0;
  512. }
  513. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  514. int i;
  515. for(i=0; i<count; i++){
  516. int r= func(c, (char*)arg + i*size);
  517. if(ret) ret[i]= r;
  518. }
  519. return 0;
  520. }
  521. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  522. int i;
  523. for(i=0; i<count; i++){
  524. int r= func(c, arg, i, 0);
  525. if(ret) ret[i]= r;
  526. }
  527. return 0;
  528. }
  529. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  530. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  531. ++fmt;
  532. return fmt[0];
  533. }
  534. void avcodec_get_frame_defaults(AVFrame *pic){
  535. memset(pic, 0, sizeof(AVFrame));
  536. pic->pts= AV_NOPTS_VALUE;
  537. pic->key_frame= 1;
  538. pic->sample_aspect_ratio = (AVRational){0, 1};
  539. pic->format = -1; /* unknown */
  540. }
  541. AVFrame *avcodec_alloc_frame(void){
  542. AVFrame *pic= av_malloc(sizeof(AVFrame));
  543. if(pic==NULL) return NULL;
  544. avcodec_get_frame_defaults(pic);
  545. return pic;
  546. }
  547. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  548. {
  549. int ret = 0;
  550. AVDictionary *tmp = NULL;
  551. if (avcodec_is_open(avctx))
  552. return 0;
  553. if ((!codec && !avctx->codec)) {
  554. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  555. return AVERROR(EINVAL);
  556. }
  557. if ((codec && avctx->codec && codec != avctx->codec)) {
  558. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  559. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  560. return AVERROR(EINVAL);
  561. }
  562. if (!codec)
  563. codec = avctx->codec;
  564. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  565. return AVERROR(EINVAL);
  566. if (options)
  567. av_dict_copy(&tmp, *options, 0);
  568. /* If there is a user-supplied mutex locking routine, call it. */
  569. if (ff_lockmgr_cb) {
  570. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  571. return -1;
  572. }
  573. entangled_thread_counter++;
  574. if(entangled_thread_counter != 1){
  575. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  576. ret = -1;
  577. goto end;
  578. }
  579. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  580. if (!avctx->internal) {
  581. ret = AVERROR(ENOMEM);
  582. goto end;
  583. }
  584. if (codec->priv_data_size > 0) {
  585. if(!avctx->priv_data){
  586. avctx->priv_data = av_mallocz(codec->priv_data_size);
  587. if (!avctx->priv_data) {
  588. ret = AVERROR(ENOMEM);
  589. goto end;
  590. }
  591. if (codec->priv_class) {
  592. *(AVClass**)avctx->priv_data= codec->priv_class;
  593. av_opt_set_defaults(avctx->priv_data);
  594. }
  595. }
  596. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  597. goto free_and_end;
  598. } else {
  599. avctx->priv_data = NULL;
  600. }
  601. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  602. goto free_and_end;
  603. if(avctx->coded_width && avctx->coded_height)
  604. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  605. else if(avctx->width && avctx->height)
  606. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  607. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  608. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  609. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  610. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  611. avcodec_set_dimensions(avctx, 0, 0);
  612. }
  613. /* if the decoder init function was already called previously,
  614. free the already allocated subtitle_header before overwriting it */
  615. if (codec_is_decoder(codec))
  616. av_freep(&avctx->subtitle_header);
  617. #define SANE_NB_CHANNELS 128U
  618. if (avctx->channels > SANE_NB_CHANNELS) {
  619. ret = AVERROR(EINVAL);
  620. goto free_and_end;
  621. }
  622. avctx->codec = codec;
  623. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  624. avctx->codec_id == CODEC_ID_NONE) {
  625. avctx->codec_type = codec->type;
  626. avctx->codec_id = codec->id;
  627. }
  628. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  629. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  630. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  631. ret = AVERROR(EINVAL);
  632. goto free_and_end;
  633. }
  634. avctx->frame_number = 0;
  635. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  636. (!avctx->time_base.num || !avctx->time_base.den)) {
  637. avctx->time_base.num = 1;
  638. avctx->time_base.den = avctx->sample_rate;
  639. }
  640. if (HAVE_THREADS && !avctx->thread_opaque) {
  641. ret = ff_thread_init(avctx);
  642. if (ret < 0) {
  643. goto free_and_end;
  644. }
  645. }
  646. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  647. avctx->thread_count = 1;
  648. if (avctx->codec->max_lowres < avctx->lowres) {
  649. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  650. avctx->codec->max_lowres);
  651. ret = AVERROR(EINVAL);
  652. goto free_and_end;
  653. }
  654. if (codec_is_encoder(avctx->codec)) {
  655. int i;
  656. if (avctx->codec->sample_fmts) {
  657. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  658. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  659. break;
  660. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  661. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  662. ret = AVERROR(EINVAL);
  663. goto free_and_end;
  664. }
  665. }
  666. if (avctx->codec->supported_samplerates) {
  667. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  668. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  669. break;
  670. if (avctx->codec->supported_samplerates[i] == 0) {
  671. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  672. ret = AVERROR(EINVAL);
  673. goto free_and_end;
  674. }
  675. }
  676. if (avctx->codec->channel_layouts) {
  677. if (!avctx->channel_layout) {
  678. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  679. } else {
  680. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  681. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  682. break;
  683. if (avctx->codec->channel_layouts[i] == 0) {
  684. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  685. ret = AVERROR(EINVAL);
  686. goto free_and_end;
  687. }
  688. }
  689. }
  690. if (avctx->channel_layout && avctx->channels) {
  691. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  692. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  693. ret = AVERROR(EINVAL);
  694. goto free_and_end;
  695. }
  696. } else if (avctx->channel_layout) {
  697. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  698. }
  699. }
  700. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  701. ret = avctx->codec->init(avctx);
  702. if (ret < 0) {
  703. goto free_and_end;
  704. }
  705. }
  706. end:
  707. entangled_thread_counter--;
  708. /* Release any user-supplied mutex. */
  709. if (ff_lockmgr_cb) {
  710. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  711. }
  712. if (options) {
  713. av_dict_free(options);
  714. *options = tmp;
  715. }
  716. return ret;
  717. free_and_end:
  718. av_dict_free(&tmp);
  719. av_freep(&avctx->priv_data);
  720. av_freep(&avctx->internal);
  721. avctx->codec= NULL;
  722. goto end;
  723. }
  724. int ff_alloc_packet(AVPacket *avpkt, int size)
  725. {
  726. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  727. return AVERROR(EINVAL);
  728. if (avpkt->data) {
  729. uint8_t *pkt_data;
  730. int pkt_size;
  731. if (avpkt->size < size)
  732. return AVERROR(EINVAL);
  733. pkt_data = avpkt->data;
  734. pkt_size = avpkt->size;
  735. av_init_packet(avpkt);
  736. avpkt->data = pkt_data;
  737. avpkt->size = pkt_size;
  738. return 0;
  739. } else {
  740. return av_new_packet(avpkt, size);
  741. }
  742. }
  743. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  744. AVPacket *avpkt,
  745. const AVFrame *frame,
  746. int *got_packet_ptr)
  747. {
  748. int ret;
  749. int user_packet = !!avpkt->data;
  750. int nb_samples;
  751. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  752. av_init_packet(avpkt);
  753. avpkt->size = 0;
  754. return 0;
  755. }
  756. /* check for valid frame size */
  757. if (frame) {
  758. nb_samples = frame->nb_samples;
  759. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  760. if (nb_samples > avctx->frame_size)
  761. return AVERROR(EINVAL);
  762. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  763. if (nb_samples != avctx->frame_size)
  764. return AVERROR(EINVAL);
  765. }
  766. } else {
  767. nb_samples = avctx->frame_size;
  768. }
  769. if (avctx->codec->encode2) {
  770. *got_packet_ptr = 0;
  771. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  772. if (!ret && *got_packet_ptr &&
  773. !(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  774. avpkt->pts = frame->pts;
  775. avpkt->duration = av_rescale_q(frame->nb_samples,
  776. (AVRational){ 1, avctx->sample_rate },
  777. avctx->time_base);
  778. }
  779. } else {
  780. /* for compatibility with encoders not supporting encode2(), we need to
  781. allocate a packet buffer if the user has not provided one or check
  782. the size otherwise */
  783. int fs_tmp = 0;
  784. int buf_size = avpkt->size;
  785. if (!user_packet) {
  786. if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
  787. av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
  788. buf_size = nb_samples * avctx->channels *
  789. av_get_bits_per_sample(avctx->codec_id) / 8;
  790. } else {
  791. /* this is a guess as to the required size.
  792. if an encoder needs more than this, it should probably
  793. implement encode2() */
  794. buf_size = 2 * avctx->frame_size * avctx->channels *
  795. av_get_bytes_per_sample(avctx->sample_fmt);
  796. buf_size += FF_MIN_BUFFER_SIZE;
  797. }
  798. }
  799. if ((ret = ff_alloc_packet(avpkt, buf_size)))
  800. return ret;
  801. /* Encoders using AVCodec.encode() that support
  802. CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
  803. the smaller size when encoding the last frame.
  804. This code can be removed once all encoders supporting
  805. CODEC_CAP_SMALL_LAST_FRAME use encode2() */
  806. if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
  807. nb_samples < avctx->frame_size) {
  808. fs_tmp = avctx->frame_size;
  809. avctx->frame_size = nb_samples;
  810. }
  811. /* encode the frame */
  812. ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
  813. frame ? frame->data[0] : NULL);
  814. if (ret >= 0) {
  815. if (!ret) {
  816. /* no output. if the packet data was allocated by libavcodec,
  817. free it */
  818. if (!user_packet)
  819. av_freep(&avpkt->data);
  820. } else {
  821. if (avctx->coded_frame)
  822. avpkt->pts = avctx->coded_frame->pts;
  823. /* Set duration for final small packet. This can be removed
  824. once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
  825. encode2() */
  826. if (fs_tmp) {
  827. avpkt->duration = av_rescale_q(avctx->frame_size,
  828. (AVRational){ 1, avctx->sample_rate },
  829. avctx->time_base);
  830. }
  831. }
  832. avpkt->size = ret;
  833. *got_packet_ptr = (ret > 0);
  834. ret = 0;
  835. }
  836. if (fs_tmp)
  837. avctx->frame_size = fs_tmp;
  838. }
  839. if (!ret)
  840. avctx->frame_number++;
  841. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  842. this needs to be moved to the encoders, but for now we can do it
  843. here to simplify things */
  844. avpkt->flags |= AV_PKT_FLAG_KEY;
  845. return ret;
  846. }
  847. #if FF_API_OLD_DECODE_AUDIO
  848. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  849. uint8_t *buf, int buf_size,
  850. const short *samples)
  851. {
  852. AVPacket pkt;
  853. AVFrame frame0;
  854. AVFrame *frame;
  855. int ret, samples_size, got_packet;
  856. av_init_packet(&pkt);
  857. pkt.data = buf;
  858. pkt.size = buf_size;
  859. if (samples) {
  860. frame = &frame0;
  861. avcodec_get_frame_defaults(frame);
  862. if (avctx->frame_size) {
  863. frame->nb_samples = avctx->frame_size;
  864. } else {
  865. /* if frame_size is not set, the number of samples must be
  866. calculated from the buffer size */
  867. int64_t nb_samples;
  868. if (!av_get_bits_per_sample(avctx->codec_id)) {
  869. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  870. "support this codec\n");
  871. return AVERROR(EINVAL);
  872. }
  873. nb_samples = (int64_t)buf_size * 8 /
  874. (av_get_bits_per_sample(avctx->codec_id) *
  875. avctx->channels);
  876. if (nb_samples >= INT_MAX)
  877. return AVERROR(EINVAL);
  878. frame->nb_samples = nb_samples;
  879. }
  880. /* it is assumed that the samples buffer is large enough based on the
  881. relevant parameters */
  882. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  883. frame->nb_samples,
  884. avctx->sample_fmt, 1);
  885. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  886. avctx->sample_fmt,
  887. samples, samples_size, 1)))
  888. return ret;
  889. /* fabricate frame pts from sample count.
  890. this is needed because the avcodec_encode_audio() API does not have
  891. a way for the user to provide pts */
  892. frame->pts = av_rescale_q(avctx->internal->sample_count,
  893. (AVRational){ 1, avctx->sample_rate },
  894. avctx->time_base);
  895. avctx->internal->sample_count += frame->nb_samples;
  896. } else {
  897. frame = NULL;
  898. }
  899. got_packet = 0;
  900. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  901. if (!ret && got_packet && avctx->coded_frame) {
  902. avctx->coded_frame->pts = pkt.pts;
  903. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  904. }
  905. /* free any side data since we cannot return it */
  906. if (pkt.side_data_elems > 0) {
  907. int i;
  908. for (i = 0; i < pkt.side_data_elems; i++)
  909. av_free(pkt.side_data[i].data);
  910. av_freep(&pkt.side_data);
  911. pkt.side_data_elems = 0;
  912. }
  913. if (frame && frame->extended_data != frame->data)
  914. av_free(frame->extended_data);
  915. return ret ? ret : pkt.size;
  916. }
  917. #endif
  918. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  919. const AVFrame *pict)
  920. {
  921. if(buf_size < FF_MIN_BUFFER_SIZE){
  922. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  923. return -1;
  924. }
  925. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  926. return -1;
  927. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  928. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  929. avctx->frame_number++;
  930. emms_c(); //needed to avoid an emms_c() call before every return;
  931. return ret;
  932. }else
  933. return 0;
  934. }
  935. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  936. const AVSubtitle *sub)
  937. {
  938. int ret;
  939. if(sub->start_display_time) {
  940. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  941. return -1;
  942. }
  943. if(sub->num_rects == 0 || !sub->rects)
  944. return -1;
  945. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  946. avctx->frame_number++;
  947. return ret;
  948. }
  949. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  950. {
  951. int size = 0;
  952. const uint8_t *data;
  953. uint32_t flags;
  954. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  955. return;
  956. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  957. if (!data || size < 4)
  958. return;
  959. flags = bytestream_get_le32(&data);
  960. size -= 4;
  961. if (size < 4) /* Required for any of the changes */
  962. return;
  963. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  964. avctx->channels = bytestream_get_le32(&data);
  965. size -= 4;
  966. }
  967. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  968. if (size < 8)
  969. return;
  970. avctx->channel_layout = bytestream_get_le64(&data);
  971. size -= 8;
  972. }
  973. if (size < 4)
  974. return;
  975. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  976. avctx->sample_rate = bytestream_get_le32(&data);
  977. size -= 4;
  978. }
  979. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  980. if (size < 8)
  981. return;
  982. avctx->width = bytestream_get_le32(&data);
  983. avctx->height = bytestream_get_le32(&data);
  984. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  985. size -= 8;
  986. }
  987. }
  988. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  989. int *got_picture_ptr,
  990. AVPacket *avpkt)
  991. {
  992. int ret;
  993. *got_picture_ptr= 0;
  994. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  995. return -1;
  996. avctx->pkt = avpkt;
  997. apply_param_change(avctx, avpkt);
  998. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  999. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1000. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1001. avpkt);
  1002. else {
  1003. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1004. avpkt);
  1005. picture->pkt_dts= avpkt->dts;
  1006. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1007. picture->width = avctx->width;
  1008. picture->height = avctx->height;
  1009. picture->format = avctx->pix_fmt;
  1010. }
  1011. emms_c(); //needed to avoid an emms_c() call before every return;
  1012. if (*got_picture_ptr)
  1013. avctx->frame_number++;
  1014. }else
  1015. ret= 0;
  1016. return ret;
  1017. }
  1018. #if FF_API_OLD_DECODE_AUDIO
  1019. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1020. int *frame_size_ptr,
  1021. AVPacket *avpkt)
  1022. {
  1023. AVFrame frame;
  1024. int ret, got_frame = 0;
  1025. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1026. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1027. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1028. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1029. "avcodec_decode_audio4()\n");
  1030. avctx->get_buffer = avcodec_default_get_buffer;
  1031. }
  1032. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1033. if (ret >= 0 && got_frame) {
  1034. int ch, plane_size;
  1035. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1036. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1037. frame.nb_samples,
  1038. avctx->sample_fmt, 1);
  1039. if (*frame_size_ptr < data_size) {
  1040. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1041. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1042. return AVERROR(EINVAL);
  1043. }
  1044. memcpy(samples, frame.extended_data[0], plane_size);
  1045. if (planar && avctx->channels > 1) {
  1046. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1047. for (ch = 1; ch < avctx->channels; ch++) {
  1048. memcpy(out, frame.extended_data[ch], plane_size);
  1049. out += plane_size;
  1050. }
  1051. }
  1052. *frame_size_ptr = data_size;
  1053. } else {
  1054. *frame_size_ptr = 0;
  1055. }
  1056. return ret;
  1057. }
  1058. #endif
  1059. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1060. AVFrame *frame,
  1061. int *got_frame_ptr,
  1062. AVPacket *avpkt)
  1063. {
  1064. int ret = 0;
  1065. *got_frame_ptr = 0;
  1066. avctx->pkt = avpkt;
  1067. if (!avpkt->data && avpkt->size) {
  1068. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1069. return AVERROR(EINVAL);
  1070. }
  1071. apply_param_change(avctx, avpkt);
  1072. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1073. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1074. if (ret >= 0 && *got_frame_ptr) {
  1075. avctx->frame_number++;
  1076. frame->pkt_dts = avpkt->dts;
  1077. if (frame->format == AV_SAMPLE_FMT_NONE)
  1078. frame->format = avctx->sample_fmt;
  1079. }
  1080. }
  1081. return ret;
  1082. }
  1083. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1084. int *got_sub_ptr,
  1085. AVPacket *avpkt)
  1086. {
  1087. int ret;
  1088. avctx->pkt = avpkt;
  1089. *got_sub_ptr = 0;
  1090. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1091. if (*got_sub_ptr)
  1092. avctx->frame_number++;
  1093. return ret;
  1094. }
  1095. void avsubtitle_free(AVSubtitle *sub)
  1096. {
  1097. int i;
  1098. for (i = 0; i < sub->num_rects; i++)
  1099. {
  1100. av_freep(&sub->rects[i]->pict.data[0]);
  1101. av_freep(&sub->rects[i]->pict.data[1]);
  1102. av_freep(&sub->rects[i]->pict.data[2]);
  1103. av_freep(&sub->rects[i]->pict.data[3]);
  1104. av_freep(&sub->rects[i]->text);
  1105. av_freep(&sub->rects[i]->ass);
  1106. av_freep(&sub->rects[i]);
  1107. }
  1108. av_freep(&sub->rects);
  1109. memset(sub, 0, sizeof(AVSubtitle));
  1110. }
  1111. av_cold int avcodec_close(AVCodecContext *avctx)
  1112. {
  1113. /* If there is a user-supplied mutex locking routine, call it. */
  1114. if (ff_lockmgr_cb) {
  1115. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1116. return -1;
  1117. }
  1118. entangled_thread_counter++;
  1119. if(entangled_thread_counter != 1){
  1120. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1121. entangled_thread_counter--;
  1122. return -1;
  1123. }
  1124. if (avcodec_is_open(avctx)) {
  1125. if (HAVE_THREADS && avctx->thread_opaque)
  1126. ff_thread_free(avctx);
  1127. if (avctx->codec && avctx->codec->close)
  1128. avctx->codec->close(avctx);
  1129. avcodec_default_free_buffers(avctx);
  1130. avctx->coded_frame = NULL;
  1131. av_freep(&avctx->internal);
  1132. }
  1133. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1134. av_opt_free(avctx->priv_data);
  1135. av_opt_free(avctx);
  1136. av_freep(&avctx->priv_data);
  1137. if (codec_is_encoder(avctx->codec))
  1138. av_freep(&avctx->extradata);
  1139. avctx->codec = NULL;
  1140. avctx->active_thread_type = 0;
  1141. entangled_thread_counter--;
  1142. /* Release any user-supplied mutex. */
  1143. if (ff_lockmgr_cb) {
  1144. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1145. }
  1146. return 0;
  1147. }
  1148. AVCodec *avcodec_find_encoder(enum CodecID id)
  1149. {
  1150. AVCodec *p, *experimental=NULL;
  1151. p = first_avcodec;
  1152. while (p) {
  1153. if (codec_is_encoder(p) && p->id == id) {
  1154. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1155. experimental = p;
  1156. } else
  1157. return p;
  1158. }
  1159. p = p->next;
  1160. }
  1161. return experimental;
  1162. }
  1163. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1164. {
  1165. AVCodec *p;
  1166. if (!name)
  1167. return NULL;
  1168. p = first_avcodec;
  1169. while (p) {
  1170. if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
  1171. return p;
  1172. p = p->next;
  1173. }
  1174. return NULL;
  1175. }
  1176. AVCodec *avcodec_find_decoder(enum CodecID id)
  1177. {
  1178. AVCodec *p;
  1179. p = first_avcodec;
  1180. while (p) {
  1181. if (codec_is_decoder(p) && p->id == id)
  1182. return p;
  1183. p = p->next;
  1184. }
  1185. return NULL;
  1186. }
  1187. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1188. {
  1189. AVCodec *p;
  1190. if (!name)
  1191. return NULL;
  1192. p = first_avcodec;
  1193. while (p) {
  1194. if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
  1195. return p;
  1196. p = p->next;
  1197. }
  1198. return NULL;
  1199. }
  1200. static int get_bit_rate(AVCodecContext *ctx)
  1201. {
  1202. int bit_rate;
  1203. int bits_per_sample;
  1204. switch(ctx->codec_type) {
  1205. case AVMEDIA_TYPE_VIDEO:
  1206. case AVMEDIA_TYPE_DATA:
  1207. case AVMEDIA_TYPE_SUBTITLE:
  1208. case AVMEDIA_TYPE_ATTACHMENT:
  1209. bit_rate = ctx->bit_rate;
  1210. break;
  1211. case AVMEDIA_TYPE_AUDIO:
  1212. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1213. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1214. break;
  1215. default:
  1216. bit_rate = 0;
  1217. break;
  1218. }
  1219. return bit_rate;
  1220. }
  1221. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1222. {
  1223. int i, len, ret = 0;
  1224. for (i = 0; i < 4; i++) {
  1225. len = snprintf(buf, buf_size,
  1226. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1227. buf += len;
  1228. buf_size = buf_size > len ? buf_size - len : 0;
  1229. ret += len;
  1230. codec_tag>>=8;
  1231. }
  1232. return ret;
  1233. }
  1234. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1235. {
  1236. const char *codec_name;
  1237. const char *profile = NULL;
  1238. AVCodec *p;
  1239. char buf1[32];
  1240. int bitrate;
  1241. AVRational display_aspect_ratio;
  1242. if (encode)
  1243. p = avcodec_find_encoder(enc->codec_id);
  1244. else
  1245. p = avcodec_find_decoder(enc->codec_id);
  1246. if (p) {
  1247. codec_name = p->name;
  1248. profile = av_get_profile_name(p, enc->profile);
  1249. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  1250. /* fake mpeg2 transport stream codec (currently not
  1251. registered) */
  1252. codec_name = "mpeg2ts";
  1253. } else if (enc->codec_name[0] != '\0') {
  1254. codec_name = enc->codec_name;
  1255. } else {
  1256. /* output avi tags */
  1257. char tag_buf[32];
  1258. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1259. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1260. codec_name = buf1;
  1261. }
  1262. switch(enc->codec_type) {
  1263. case AVMEDIA_TYPE_VIDEO:
  1264. snprintf(buf, buf_size,
  1265. "Video: %s%s",
  1266. codec_name, enc->mb_decision ? " (hq)" : "");
  1267. if (profile)
  1268. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1269. " (%s)", profile);
  1270. if (enc->pix_fmt != PIX_FMT_NONE) {
  1271. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1272. ", %s",
  1273. av_get_pix_fmt_name(enc->pix_fmt));
  1274. }
  1275. if (enc->width) {
  1276. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1277. ", %dx%d",
  1278. enc->width, enc->height);
  1279. if (enc->sample_aspect_ratio.num) {
  1280. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1281. enc->width*enc->sample_aspect_ratio.num,
  1282. enc->height*enc->sample_aspect_ratio.den,
  1283. 1024*1024);
  1284. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1285. " [PAR %d:%d DAR %d:%d]",
  1286. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1287. display_aspect_ratio.num, display_aspect_ratio.den);
  1288. }
  1289. if(av_log_get_level() >= AV_LOG_DEBUG){
  1290. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1291. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1292. ", %d/%d",
  1293. enc->time_base.num/g, enc->time_base.den/g);
  1294. }
  1295. }
  1296. if (encode) {
  1297. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1298. ", q=%d-%d", enc->qmin, enc->qmax);
  1299. }
  1300. break;
  1301. case AVMEDIA_TYPE_AUDIO:
  1302. snprintf(buf, buf_size,
  1303. "Audio: %s",
  1304. codec_name);
  1305. if (profile)
  1306. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1307. " (%s)", profile);
  1308. if (enc->sample_rate) {
  1309. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1310. ", %d Hz", enc->sample_rate);
  1311. }
  1312. av_strlcat(buf, ", ", buf_size);
  1313. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1314. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1315. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1316. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1317. }
  1318. break;
  1319. case AVMEDIA_TYPE_DATA:
  1320. snprintf(buf, buf_size, "Data: %s", codec_name);
  1321. break;
  1322. case AVMEDIA_TYPE_SUBTITLE:
  1323. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1324. break;
  1325. case AVMEDIA_TYPE_ATTACHMENT:
  1326. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1327. break;
  1328. default:
  1329. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1330. return;
  1331. }
  1332. if (encode) {
  1333. if (enc->flags & CODEC_FLAG_PASS1)
  1334. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1335. ", pass 1");
  1336. if (enc->flags & CODEC_FLAG_PASS2)
  1337. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1338. ", pass 2");
  1339. }
  1340. bitrate = get_bit_rate(enc);
  1341. if (bitrate != 0) {
  1342. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1343. ", %d kb/s", bitrate / 1000);
  1344. }
  1345. }
  1346. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1347. {
  1348. const AVProfile *p;
  1349. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1350. return NULL;
  1351. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1352. if (p->profile == profile)
  1353. return p->name;
  1354. return NULL;
  1355. }
  1356. unsigned avcodec_version( void )
  1357. {
  1358. return LIBAVCODEC_VERSION_INT;
  1359. }
  1360. const char *avcodec_configuration(void)
  1361. {
  1362. return LIBAV_CONFIGURATION;
  1363. }
  1364. const char *avcodec_license(void)
  1365. {
  1366. #define LICENSE_PREFIX "libavcodec license: "
  1367. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1368. }
  1369. void avcodec_flush_buffers(AVCodecContext *avctx)
  1370. {
  1371. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1372. ff_thread_flush(avctx);
  1373. else if(avctx->codec->flush)
  1374. avctx->codec->flush(avctx);
  1375. }
  1376. static void video_free_buffers(AVCodecContext *s)
  1377. {
  1378. AVCodecInternal *avci = s->internal;
  1379. int i, j;
  1380. if (!avci->buffer)
  1381. return;
  1382. if (avci->buffer_count)
  1383. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1384. avci->buffer_count);
  1385. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1386. InternalBuffer *buf = &avci->buffer[i];
  1387. for(j=0; j<4; j++){
  1388. av_freep(&buf->base[j]);
  1389. buf->data[j]= NULL;
  1390. }
  1391. }
  1392. av_freep(&avci->buffer);
  1393. avci->buffer_count=0;
  1394. }
  1395. static void audio_free_buffers(AVCodecContext *avctx)
  1396. {
  1397. AVCodecInternal *avci = avctx->internal;
  1398. InternalBuffer *buf;
  1399. if (!avci->buffer)
  1400. return;
  1401. buf = avci->buffer;
  1402. if (buf->extended_data) {
  1403. av_free(buf->extended_data[0]);
  1404. if (buf->extended_data != buf->data)
  1405. av_free(buf->extended_data);
  1406. }
  1407. av_freep(&avci->buffer);
  1408. }
  1409. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1410. {
  1411. switch (avctx->codec_type) {
  1412. case AVMEDIA_TYPE_VIDEO:
  1413. video_free_buffers(avctx);
  1414. break;
  1415. case AVMEDIA_TYPE_AUDIO:
  1416. audio_free_buffers(avctx);
  1417. break;
  1418. default:
  1419. break;
  1420. }
  1421. }
  1422. int av_get_bits_per_sample(enum CodecID codec_id){
  1423. switch(codec_id){
  1424. case CODEC_ID_ADPCM_SBPRO_2:
  1425. return 2;
  1426. case CODEC_ID_ADPCM_SBPRO_3:
  1427. return 3;
  1428. case CODEC_ID_ADPCM_SBPRO_4:
  1429. case CODEC_ID_ADPCM_CT:
  1430. case CODEC_ID_ADPCM_IMA_APC:
  1431. case CODEC_ID_ADPCM_IMA_WAV:
  1432. case CODEC_ID_ADPCM_IMA_QT:
  1433. case CODEC_ID_ADPCM_SWF:
  1434. case CODEC_ID_ADPCM_MS:
  1435. case CODEC_ID_ADPCM_YAMAHA:
  1436. case CODEC_ID_ADPCM_G722:
  1437. return 4;
  1438. case CODEC_ID_PCM_ALAW:
  1439. case CODEC_ID_PCM_MULAW:
  1440. case CODEC_ID_PCM_S8:
  1441. case CODEC_ID_PCM_U8:
  1442. case CODEC_ID_PCM_ZORK:
  1443. return 8;
  1444. case CODEC_ID_PCM_S16BE:
  1445. case CODEC_ID_PCM_S16LE:
  1446. case CODEC_ID_PCM_S16LE_PLANAR:
  1447. case CODEC_ID_PCM_U16BE:
  1448. case CODEC_ID_PCM_U16LE:
  1449. return 16;
  1450. case CODEC_ID_PCM_S24DAUD:
  1451. case CODEC_ID_PCM_S24BE:
  1452. case CODEC_ID_PCM_S24LE:
  1453. case CODEC_ID_PCM_U24BE:
  1454. case CODEC_ID_PCM_U24LE:
  1455. return 24;
  1456. case CODEC_ID_PCM_S32BE:
  1457. case CODEC_ID_PCM_S32LE:
  1458. case CODEC_ID_PCM_U32BE:
  1459. case CODEC_ID_PCM_U32LE:
  1460. case CODEC_ID_PCM_F32BE:
  1461. case CODEC_ID_PCM_F32LE:
  1462. return 32;
  1463. case CODEC_ID_PCM_F64BE:
  1464. case CODEC_ID_PCM_F64LE:
  1465. return 64;
  1466. default:
  1467. return 0;
  1468. }
  1469. }
  1470. #if !HAVE_THREADS
  1471. int ff_thread_init(AVCodecContext *s){
  1472. return -1;
  1473. }
  1474. #endif
  1475. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1476. {
  1477. unsigned int n = 0;
  1478. while(v >= 0xff) {
  1479. *s++ = 0xff;
  1480. v -= 0xff;
  1481. n++;
  1482. }
  1483. *s = v;
  1484. n++;
  1485. return n;
  1486. }
  1487. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1488. int i;
  1489. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1490. return i;
  1491. }
  1492. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1493. {
  1494. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
  1495. "version to the newest one from Git. If the problem still "
  1496. "occurs, it means that your file has a feature which has not "
  1497. "been implemented.\n", feature);
  1498. if(want_sample)
  1499. av_log_ask_for_sample(avc, NULL);
  1500. }
  1501. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1502. {
  1503. va_list argument_list;
  1504. va_start(argument_list, msg);
  1505. if (msg)
  1506. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1507. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1508. "of this file to ftp://upload.libav.org/incoming/ "
  1509. "and contact the libav-devel mailing list.\n");
  1510. va_end(argument_list);
  1511. }
  1512. static AVHWAccel *first_hwaccel = NULL;
  1513. void av_register_hwaccel(AVHWAccel *hwaccel)
  1514. {
  1515. AVHWAccel **p = &first_hwaccel;
  1516. while (*p)
  1517. p = &(*p)->next;
  1518. *p = hwaccel;
  1519. hwaccel->next = NULL;
  1520. }
  1521. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1522. {
  1523. return hwaccel ? hwaccel->next : first_hwaccel;
  1524. }
  1525. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1526. {
  1527. AVHWAccel *hwaccel=NULL;
  1528. while((hwaccel= av_hwaccel_next(hwaccel))){
  1529. if ( hwaccel->id == codec_id
  1530. && hwaccel->pix_fmt == pix_fmt)
  1531. return hwaccel;
  1532. }
  1533. return NULL;
  1534. }
  1535. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1536. {
  1537. if (ff_lockmgr_cb) {
  1538. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1539. return -1;
  1540. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1541. return -1;
  1542. }
  1543. ff_lockmgr_cb = cb;
  1544. if (ff_lockmgr_cb) {
  1545. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1546. return -1;
  1547. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1548. return -1;
  1549. }
  1550. return 0;
  1551. }
  1552. int avpriv_lock_avformat(void)
  1553. {
  1554. if (ff_lockmgr_cb) {
  1555. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1556. return -1;
  1557. }
  1558. return 0;
  1559. }
  1560. int avpriv_unlock_avformat(void)
  1561. {
  1562. if (ff_lockmgr_cb) {
  1563. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1564. return -1;
  1565. }
  1566. return 0;
  1567. }
  1568. unsigned int avpriv_toupper4(unsigned int x)
  1569. {
  1570. return toupper( x &0xFF)
  1571. + (toupper((x>>8 )&0xFF)<<8 )
  1572. + (toupper((x>>16)&0xFF)<<16)
  1573. + (toupper((x>>24)&0xFF)<<24);
  1574. }
  1575. #if !HAVE_THREADS
  1576. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1577. {
  1578. f->owner = avctx;
  1579. return avctx->get_buffer(avctx, f);
  1580. }
  1581. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1582. {
  1583. f->owner->release_buffer(f->owner, f);
  1584. }
  1585. void ff_thread_finish_setup(AVCodecContext *avctx)
  1586. {
  1587. }
  1588. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1589. {
  1590. }
  1591. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1592. {
  1593. }
  1594. #endif
  1595. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1596. {
  1597. if (codec_id <= CODEC_ID_NONE)
  1598. return AVMEDIA_TYPE_UNKNOWN;
  1599. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1600. return AVMEDIA_TYPE_VIDEO;
  1601. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1602. return AVMEDIA_TYPE_AUDIO;
  1603. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1604. return AVMEDIA_TYPE_SUBTITLE;
  1605. return AVMEDIA_TYPE_UNKNOWN;
  1606. }
  1607. int avcodec_is_open(AVCodecContext *s)
  1608. {
  1609. return !!s->internal;
  1610. }