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.

1267 lines
37KB

  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/avstring.h"
  27. #include "libavutil/integer.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/audioconvert.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #include "libavutil/opt.h"
  36. #include "imgconvert.h"
  37. #include "thread.h"
  38. #include "audioconvert.h"
  39. #include "internal.h"
  40. #include <stdlib.h>
  41. #include <stdarg.h>
  42. #include <limits.h>
  43. #include <float.h>
  44. static int volatile entangled_thread_counter=0;
  45. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  46. static void *codec_mutex;
  47. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  48. {
  49. if(min_size < *size)
  50. return ptr;
  51. min_size= FFMAX(17*min_size/16 + 32, min_size);
  52. ptr= av_realloc(ptr, min_size);
  53. 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
  54. min_size= 0;
  55. *size= min_size;
  56. return ptr;
  57. }
  58. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  59. {
  60. void **p = ptr;
  61. if (min_size < *size)
  62. return;
  63. min_size= FFMAX(17*min_size/16 + 32, min_size);
  64. av_free(*p);
  65. *p = av_malloc(min_size);
  66. if (!*p) min_size = 0;
  67. *size= min_size;
  68. }
  69. /* encoder management */
  70. static AVCodec *first_avcodec = NULL;
  71. AVCodec *av_codec_next(AVCodec *c){
  72. if(c) return c->next;
  73. else return first_avcodec;
  74. }
  75. void avcodec_register(AVCodec *codec)
  76. {
  77. AVCodec **p;
  78. avcodec_init();
  79. p = &first_avcodec;
  80. while (*p != NULL) p = &(*p)->next;
  81. *p = codec;
  82. codec->next = NULL;
  83. }
  84. unsigned avcodec_get_edge_width(void)
  85. {
  86. return EDGE_WIDTH;
  87. }
  88. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  89. s->coded_width = width;
  90. s->coded_height= height;
  91. s->width = -((-width )>>s->lowres);
  92. s->height= -((-height)>>s->lowres);
  93. }
  94. typedef struct InternalBuffer{
  95. int last_pic_num;
  96. uint8_t *base[4];
  97. uint8_t *data[4];
  98. int linesize[4];
  99. int width, height;
  100. enum PixelFormat pix_fmt;
  101. }InternalBuffer;
  102. #define INTERNAL_BUFFER_SIZE 32
  103. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  104. int w_align= 1;
  105. int h_align= 1;
  106. switch(s->pix_fmt){
  107. case PIX_FMT_YUV420P:
  108. case PIX_FMT_YUYV422:
  109. case PIX_FMT_UYVY422:
  110. case PIX_FMT_YUV422P:
  111. case PIX_FMT_YUV440P:
  112. case PIX_FMT_YUV444P:
  113. case PIX_FMT_GRAY8:
  114. case PIX_FMT_GRAY16BE:
  115. case PIX_FMT_GRAY16LE:
  116. case PIX_FMT_YUVJ420P:
  117. case PIX_FMT_YUVJ422P:
  118. case PIX_FMT_YUVJ440P:
  119. case PIX_FMT_YUVJ444P:
  120. case PIX_FMT_YUVA420P:
  121. case PIX_FMT_YUV420P9LE:
  122. case PIX_FMT_YUV420P9BE:
  123. case PIX_FMT_YUV420P10LE:
  124. case PIX_FMT_YUV420P10BE:
  125. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  126. h_align= 16;
  127. if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264)
  128. h_align= 32; // interlaced is rounded up to 2 MBs
  129. break;
  130. case PIX_FMT_YUV411P:
  131. case PIX_FMT_UYYVYY411:
  132. w_align=32;
  133. h_align=8;
  134. break;
  135. case PIX_FMT_YUV410P:
  136. if(s->codec_id == CODEC_ID_SVQ1){
  137. w_align=64;
  138. h_align=64;
  139. }
  140. case PIX_FMT_RGB555:
  141. if(s->codec_id == CODEC_ID_RPZA){
  142. w_align=4;
  143. h_align=4;
  144. }
  145. case PIX_FMT_PAL8:
  146. case PIX_FMT_BGR8:
  147. case PIX_FMT_RGB8:
  148. if(s->codec_id == CODEC_ID_SMC){
  149. w_align=4;
  150. h_align=4;
  151. }
  152. break;
  153. case PIX_FMT_BGR24:
  154. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  155. w_align=4;
  156. h_align=4;
  157. }
  158. break;
  159. default:
  160. w_align= 1;
  161. h_align= 1;
  162. break;
  163. }
  164. *width = FFALIGN(*width , w_align);
  165. *height= FFALIGN(*height, h_align);
  166. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  167. *height+=2; // some of the optimized chroma MC reads one line too much
  168. // which is also done in mpeg decoders with lowres > 0
  169. linesize_align[0] =
  170. linesize_align[1] =
  171. linesize_align[2] =
  172. linesize_align[3] = STRIDE_ALIGN;
  173. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  174. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  175. //picture size unneccessarily in some cases. The solution here is not
  176. //pretty and better ideas are welcome!
  177. #if HAVE_MMX
  178. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  179. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  180. s->codec_id == CODEC_ID_VP6A) {
  181. linesize_align[0] =
  182. linesize_align[1] =
  183. linesize_align[2] = 16;
  184. }
  185. #endif
  186. }
  187. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  188. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  189. int linesize_align[4];
  190. int align;
  191. avcodec_align_dimensions2(s, width, height, linesize_align);
  192. align = FFMAX(linesize_align[0], linesize_align[3]);
  193. linesize_align[1] <<= chroma_shift;
  194. linesize_align[2] <<= chroma_shift;
  195. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  196. *width=FFALIGN(*width, align);
  197. }
  198. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  199. int i;
  200. int w= s->width;
  201. int h= s->height;
  202. InternalBuffer *buf;
  203. int *picture_number;
  204. if(pic->data[0]!=NULL) {
  205. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  206. return -1;
  207. }
  208. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  209. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  210. return -1;
  211. }
  212. if(av_image_check_size(w, h, 0, s))
  213. return -1;
  214. if(s->internal_buffer==NULL){
  215. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  216. }
  217. #if 0
  218. s->internal_buffer= av_fast_realloc(
  219. s->internal_buffer,
  220. &s->internal_buffer_size,
  221. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  222. );
  223. #endif
  224. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  225. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  226. (*picture_number)++;
  227. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  228. if(s->active_thread_type&FF_THREAD_FRAME) {
  229. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  230. return -1;
  231. }
  232. for(i=0; i<4; i++){
  233. av_freep(&buf->base[i]);
  234. buf->data[i]= NULL;
  235. }
  236. }
  237. if(buf->base[0]){
  238. pic->age= *picture_number - buf->last_pic_num;
  239. buf->last_pic_num= *picture_number;
  240. }else{
  241. int h_chroma_shift, v_chroma_shift;
  242. int size[4] = {0};
  243. int tmpsize;
  244. int unaligned;
  245. AVPicture picture;
  246. int stride_align[4];
  247. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  248. avcodec_align_dimensions2(s, &w, &h, stride_align);
  249. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  250. w+= EDGE_WIDTH*2;
  251. h+= EDGE_WIDTH*2;
  252. }
  253. do {
  254. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  255. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  256. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  257. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  258. w += w & ~(w-1);
  259. unaligned = 0;
  260. for (i=0; i<4; i++){
  261. unaligned |= picture.linesize[i] % stride_align[i];
  262. }
  263. } while (unaligned);
  264. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  265. if (tmpsize < 0)
  266. return -1;
  267. for (i=0; i<3 && picture.data[i+1]; i++)
  268. size[i] = picture.data[i+1] - picture.data[i];
  269. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  270. buf->last_pic_num= -256*256*256*64;
  271. memset(buf->base, 0, sizeof(buf->base));
  272. memset(buf->data, 0, sizeof(buf->data));
  273. for(i=0; i<4 && size[i]; i++){
  274. const int h_shift= i==0 ? 0 : h_chroma_shift;
  275. const int v_shift= i==0 ? 0 : v_chroma_shift;
  276. buf->linesize[i]= picture.linesize[i];
  277. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  278. if(buf->base[i]==NULL) return -1;
  279. memset(buf->base[i], 128, size[i]);
  280. // no edge if EDGE EMU or not planar YUV
  281. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  282. buf->data[i] = buf->base[i];
  283. else
  284. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  285. }
  286. if(size[1] && !size[2])
  287. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  288. buf->width = s->width;
  289. buf->height = s->height;
  290. buf->pix_fmt= s->pix_fmt;
  291. pic->age= 256*256*256*64;
  292. }
  293. pic->type= FF_BUFFER_TYPE_INTERNAL;
  294. for(i=0; i<4; i++){
  295. pic->base[i]= buf->base[i];
  296. pic->data[i]= buf->data[i];
  297. pic->linesize[i]= buf->linesize[i];
  298. }
  299. s->internal_buffer_count++;
  300. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  301. else pic->pkt_pts= AV_NOPTS_VALUE;
  302. pic->reordered_opaque= s->reordered_opaque;
  303. if(s->debug&FF_DEBUG_BUFFERS)
  304. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  305. return 0;
  306. }
  307. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  308. int i;
  309. InternalBuffer *buf, *last;
  310. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  311. assert(s->internal_buffer_count);
  312. buf = NULL; /* avoids warning */
  313. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  314. buf= &((InternalBuffer*)s->internal_buffer)[i];
  315. if(buf->data[0] == pic->data[0])
  316. break;
  317. }
  318. assert(i < s->internal_buffer_count);
  319. s->internal_buffer_count--;
  320. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  321. FFSWAP(InternalBuffer, *buf, *last);
  322. for(i=0; i<4; i++){
  323. pic->data[i]=NULL;
  324. // pic->base[i]=NULL;
  325. }
  326. //printf("R%X\n", pic->opaque);
  327. if(s->debug&FF_DEBUG_BUFFERS)
  328. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  329. }
  330. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  331. AVFrame temp_pic;
  332. int i;
  333. /* If no picture return a new buffer */
  334. if(pic->data[0] == NULL) {
  335. /* We will copy from buffer, so must be readable */
  336. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  337. return s->get_buffer(s, pic);
  338. }
  339. /* If internal buffer type return the same buffer */
  340. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  341. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  342. else pic->pkt_pts= AV_NOPTS_VALUE;
  343. pic->reordered_opaque= s->reordered_opaque;
  344. return 0;
  345. }
  346. /*
  347. * Not internal type and reget_buffer not overridden, emulate cr buffer
  348. */
  349. temp_pic = *pic;
  350. for(i = 0; i < 4; i++)
  351. pic->data[i] = pic->base[i] = NULL;
  352. pic->opaque = NULL;
  353. /* Allocate new frame */
  354. if (s->get_buffer(s, pic))
  355. return -1;
  356. /* Copy image data from old buffer to new buffer */
  357. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  358. s->height);
  359. s->release_buffer(s, &temp_pic); // Release old frame
  360. return 0;
  361. }
  362. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  363. int i;
  364. for(i=0; i<count; i++){
  365. int r= func(c, (char*)arg + i*size);
  366. if(ret) ret[i]= r;
  367. }
  368. return 0;
  369. }
  370. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  371. int i;
  372. for(i=0; i<count; i++){
  373. int r= func(c, arg, i, 0);
  374. if(ret) ret[i]= r;
  375. }
  376. return 0;
  377. }
  378. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  379. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  380. ++fmt;
  381. return fmt[0];
  382. }
  383. void avcodec_get_frame_defaults(AVFrame *pic){
  384. memset(pic, 0, sizeof(AVFrame));
  385. pic->pts= AV_NOPTS_VALUE;
  386. pic->key_frame= 1;
  387. }
  388. AVFrame *avcodec_alloc_frame(void){
  389. AVFrame *pic= av_malloc(sizeof(AVFrame));
  390. if(pic==NULL) return NULL;
  391. avcodec_get_frame_defaults(pic);
  392. return pic;
  393. }
  394. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  395. {
  396. int ret = 0;
  397. /* If there is a user-supplied mutex locking routine, call it. */
  398. if (ff_lockmgr_cb) {
  399. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  400. return -1;
  401. }
  402. entangled_thread_counter++;
  403. if(entangled_thread_counter != 1){
  404. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  405. ret = -1;
  406. goto end;
  407. }
  408. if(avctx->codec || !codec) {
  409. ret = AVERROR(EINVAL);
  410. goto end;
  411. }
  412. if (codec->priv_data_size > 0) {
  413. if(!avctx->priv_data){
  414. avctx->priv_data = av_mallocz(codec->priv_data_size);
  415. if (!avctx->priv_data) {
  416. ret = AVERROR(ENOMEM);
  417. goto end;
  418. }
  419. if(codec->priv_class){ //this can be droped once all user apps use avcodec_get_context_defaults3()
  420. *(AVClass**)avctx->priv_data= codec->priv_class;
  421. av_opt_set_defaults(avctx->priv_data);
  422. }
  423. }
  424. } else {
  425. avctx->priv_data = NULL;
  426. }
  427. if(avctx->coded_width && avctx->coded_height)
  428. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  429. else if(avctx->width && avctx->height)
  430. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  431. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  432. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  433. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  434. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  435. avcodec_set_dimensions(avctx, 0, 0);
  436. }
  437. /* if the decoder init function was already called previously,
  438. free the already allocated subtitle_header before overwriting it */
  439. if (codec->decode)
  440. av_freep(&avctx->subtitle_header);
  441. #define SANE_NB_CHANNELS 128U
  442. if (avctx->channels > SANE_NB_CHANNELS) {
  443. ret = AVERROR(EINVAL);
  444. goto free_and_end;
  445. }
  446. avctx->codec = codec;
  447. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  448. avctx->codec_id == CODEC_ID_NONE) {
  449. avctx->codec_type = codec->type;
  450. avctx->codec_id = codec->id;
  451. }
  452. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  453. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  454. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  455. ret = AVERROR(EINVAL);
  456. goto free_and_end;
  457. }
  458. avctx->frame_number = 0;
  459. if (HAVE_THREADS && !avctx->thread_opaque) {
  460. ret = ff_thread_init(avctx);
  461. if (ret < 0) {
  462. goto free_and_end;
  463. }
  464. }
  465. if (avctx->codec->max_lowres < avctx->lowres) {
  466. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  467. avctx->codec->max_lowres);
  468. ret = AVERROR(EINVAL);
  469. goto free_and_end;
  470. }
  471. if (avctx->codec->encode) {
  472. int i;
  473. if (avctx->codec->sample_fmts) {
  474. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  475. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  476. break;
  477. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  478. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  479. ret = AVERROR(EINVAL);
  480. goto free_and_end;
  481. }
  482. }
  483. if (avctx->codec->supported_samplerates) {
  484. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  485. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  486. break;
  487. if (avctx->codec->supported_samplerates[i] == 0) {
  488. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  489. ret = AVERROR(EINVAL);
  490. goto free_and_end;
  491. }
  492. }
  493. if (avctx->codec->channel_layouts) {
  494. if (!avctx->channel_layout) {
  495. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  496. } else {
  497. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  498. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  499. break;
  500. if (avctx->codec->channel_layouts[i] == 0) {
  501. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  502. ret = AVERROR(EINVAL);
  503. goto free_and_end;
  504. }
  505. }
  506. }
  507. if (avctx->channel_layout && avctx->channels) {
  508. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  509. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  510. ret = AVERROR(EINVAL);
  511. goto free_and_end;
  512. }
  513. } else if (avctx->channel_layout) {
  514. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  515. }
  516. }
  517. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  518. ret = avctx->codec->init(avctx);
  519. if (ret < 0) {
  520. goto free_and_end;
  521. }
  522. }
  523. end:
  524. entangled_thread_counter--;
  525. /* Release any user-supplied mutex. */
  526. if (ff_lockmgr_cb) {
  527. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  528. }
  529. return ret;
  530. free_and_end:
  531. av_freep(&avctx->priv_data);
  532. avctx->codec= NULL;
  533. goto end;
  534. }
  535. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  536. const short *samples)
  537. {
  538. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  539. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  540. return -1;
  541. }
  542. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  543. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  544. avctx->frame_number++;
  545. return ret;
  546. }else
  547. return 0;
  548. }
  549. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  550. const AVFrame *pict)
  551. {
  552. if(buf_size < FF_MIN_BUFFER_SIZE){
  553. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  554. return -1;
  555. }
  556. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  557. return -1;
  558. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  559. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  560. avctx->frame_number++;
  561. emms_c(); //needed to avoid an emms_c() call before every return;
  562. return ret;
  563. }else
  564. return 0;
  565. }
  566. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  567. const AVSubtitle *sub)
  568. {
  569. int ret;
  570. if(sub->start_display_time) {
  571. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  572. return -1;
  573. }
  574. if(sub->num_rects == 0 || !sub->rects)
  575. return -1;
  576. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  577. avctx->frame_number++;
  578. return ret;
  579. }
  580. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  581. int *got_picture_ptr,
  582. AVPacket *avpkt)
  583. {
  584. int ret;
  585. *got_picture_ptr= 0;
  586. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  587. return -1;
  588. avctx->pkt = avpkt;
  589. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  590. if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  591. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  592. avpkt);
  593. else {
  594. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  595. avpkt);
  596. picture->pkt_dts= avpkt->dts;
  597. }
  598. emms_c(); //needed to avoid an emms_c() call before every return;
  599. if (*got_picture_ptr)
  600. avctx->frame_number++;
  601. }else
  602. ret= 0;
  603. return ret;
  604. }
  605. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  606. int *frame_size_ptr,
  607. AVPacket *avpkt)
  608. {
  609. int ret;
  610. avctx->pkt = avpkt;
  611. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  612. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  613. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  614. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  615. return -1;
  616. }
  617. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  618. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  619. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  620. return -1;
  621. }
  622. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  623. avctx->frame_number++;
  624. }else{
  625. ret= 0;
  626. *frame_size_ptr=0;
  627. }
  628. return ret;
  629. }
  630. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  631. int *got_sub_ptr,
  632. AVPacket *avpkt)
  633. {
  634. int ret;
  635. avctx->pkt = avpkt;
  636. *got_sub_ptr = 0;
  637. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  638. if (*got_sub_ptr)
  639. avctx->frame_number++;
  640. return ret;
  641. }
  642. void avsubtitle_free(AVSubtitle *sub)
  643. {
  644. int i;
  645. for (i = 0; i < sub->num_rects; i++)
  646. {
  647. av_freep(&sub->rects[i]->pict.data[0]);
  648. av_freep(&sub->rects[i]->pict.data[1]);
  649. av_freep(&sub->rects[i]->pict.data[2]);
  650. av_freep(&sub->rects[i]->pict.data[3]);
  651. av_freep(&sub->rects[i]->text);
  652. av_freep(&sub->rects[i]->ass);
  653. av_freep(&sub->rects[i]);
  654. }
  655. av_freep(&sub->rects);
  656. memset(sub, 0, sizeof(AVSubtitle));
  657. }
  658. av_cold int avcodec_close(AVCodecContext *avctx)
  659. {
  660. /* If there is a user-supplied mutex locking routine, call it. */
  661. if (ff_lockmgr_cb) {
  662. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  663. return -1;
  664. }
  665. entangled_thread_counter++;
  666. if(entangled_thread_counter != 1){
  667. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  668. entangled_thread_counter--;
  669. return -1;
  670. }
  671. if (HAVE_THREADS && avctx->thread_opaque)
  672. ff_thread_free(avctx);
  673. if (avctx->codec && avctx->codec->close)
  674. avctx->codec->close(avctx);
  675. avcodec_default_free_buffers(avctx);
  676. avctx->coded_frame = NULL;
  677. av_freep(&avctx->priv_data);
  678. if(avctx->codec && avctx->codec->encode)
  679. av_freep(&avctx->extradata);
  680. avctx->codec = NULL;
  681. avctx->active_thread_type = 0;
  682. entangled_thread_counter--;
  683. /* Release any user-supplied mutex. */
  684. if (ff_lockmgr_cb) {
  685. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  686. }
  687. return 0;
  688. }
  689. AVCodec *avcodec_find_encoder(enum CodecID id)
  690. {
  691. AVCodec *p, *experimental=NULL;
  692. p = first_avcodec;
  693. while (p) {
  694. if (p->encode != NULL && p->id == id) {
  695. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  696. experimental = p;
  697. } else
  698. return p;
  699. }
  700. p = p->next;
  701. }
  702. return experimental;
  703. }
  704. AVCodec *avcodec_find_encoder_by_name(const char *name)
  705. {
  706. AVCodec *p;
  707. if (!name)
  708. return NULL;
  709. p = first_avcodec;
  710. while (p) {
  711. if (p->encode != NULL && strcmp(name,p->name) == 0)
  712. return p;
  713. p = p->next;
  714. }
  715. return NULL;
  716. }
  717. AVCodec *avcodec_find_decoder(enum CodecID id)
  718. {
  719. AVCodec *p;
  720. p = first_avcodec;
  721. while (p) {
  722. if (p->decode != NULL && p->id == id)
  723. return p;
  724. p = p->next;
  725. }
  726. return NULL;
  727. }
  728. AVCodec *avcodec_find_decoder_by_name(const char *name)
  729. {
  730. AVCodec *p;
  731. if (!name)
  732. return NULL;
  733. p = first_avcodec;
  734. while (p) {
  735. if (p->decode != NULL && strcmp(name,p->name) == 0)
  736. return p;
  737. p = p->next;
  738. }
  739. return NULL;
  740. }
  741. static int get_bit_rate(AVCodecContext *ctx)
  742. {
  743. int bit_rate;
  744. int bits_per_sample;
  745. switch(ctx->codec_type) {
  746. case AVMEDIA_TYPE_VIDEO:
  747. case AVMEDIA_TYPE_DATA:
  748. case AVMEDIA_TYPE_SUBTITLE:
  749. case AVMEDIA_TYPE_ATTACHMENT:
  750. bit_rate = ctx->bit_rate;
  751. break;
  752. case AVMEDIA_TYPE_AUDIO:
  753. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  754. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  755. break;
  756. default:
  757. bit_rate = 0;
  758. break;
  759. }
  760. return bit_rate;
  761. }
  762. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  763. {
  764. int i, len, ret = 0;
  765. for (i = 0; i < 4; i++) {
  766. len = snprintf(buf, buf_size,
  767. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  768. buf += len;
  769. buf_size = buf_size > len ? buf_size - len : 0;
  770. ret += len;
  771. codec_tag>>=8;
  772. }
  773. return ret;
  774. }
  775. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  776. {
  777. const char *codec_name;
  778. const char *profile = NULL;
  779. AVCodec *p;
  780. char buf1[32];
  781. int bitrate;
  782. AVRational display_aspect_ratio;
  783. if (encode)
  784. p = avcodec_find_encoder(enc->codec_id);
  785. else
  786. p = avcodec_find_decoder(enc->codec_id);
  787. if (p) {
  788. codec_name = p->name;
  789. profile = av_get_profile_name(p, enc->profile);
  790. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  791. /* fake mpeg2 transport stream codec (currently not
  792. registered) */
  793. codec_name = "mpeg2ts";
  794. } else if (enc->codec_name[0] != '\0') {
  795. codec_name = enc->codec_name;
  796. } else {
  797. /* output avi tags */
  798. char tag_buf[32];
  799. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  800. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  801. codec_name = buf1;
  802. }
  803. switch(enc->codec_type) {
  804. case AVMEDIA_TYPE_VIDEO:
  805. snprintf(buf, buf_size,
  806. "Video: %s%s",
  807. codec_name, enc->mb_decision ? " (hq)" : "");
  808. if (profile)
  809. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  810. " (%s)", profile);
  811. if (enc->pix_fmt != PIX_FMT_NONE) {
  812. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  813. ", %s",
  814. avcodec_get_pix_fmt_name(enc->pix_fmt));
  815. }
  816. if (enc->width) {
  817. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  818. ", %dx%d",
  819. enc->width, enc->height);
  820. if (enc->sample_aspect_ratio.num) {
  821. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  822. enc->width*enc->sample_aspect_ratio.num,
  823. enc->height*enc->sample_aspect_ratio.den,
  824. 1024*1024);
  825. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  826. " [PAR %d:%d DAR %d:%d]",
  827. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  828. display_aspect_ratio.num, display_aspect_ratio.den);
  829. }
  830. if(av_log_get_level() >= AV_LOG_DEBUG){
  831. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  832. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  833. ", %d/%d",
  834. enc->time_base.num/g, enc->time_base.den/g);
  835. }
  836. }
  837. if (encode) {
  838. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  839. ", q=%d-%d", enc->qmin, enc->qmax);
  840. }
  841. break;
  842. case AVMEDIA_TYPE_AUDIO:
  843. snprintf(buf, buf_size,
  844. "Audio: %s",
  845. codec_name);
  846. if (profile)
  847. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  848. " (%s)", profile);
  849. if (enc->sample_rate) {
  850. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  851. ", %d Hz", enc->sample_rate);
  852. }
  853. av_strlcat(buf, ", ", buf_size);
  854. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  855. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  856. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  857. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  858. }
  859. break;
  860. case AVMEDIA_TYPE_DATA:
  861. snprintf(buf, buf_size, "Data: %s", codec_name);
  862. break;
  863. case AVMEDIA_TYPE_SUBTITLE:
  864. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  865. break;
  866. case AVMEDIA_TYPE_ATTACHMENT:
  867. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  868. break;
  869. default:
  870. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  871. return;
  872. }
  873. if (encode) {
  874. if (enc->flags & CODEC_FLAG_PASS1)
  875. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  876. ", pass 1");
  877. if (enc->flags & CODEC_FLAG_PASS2)
  878. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  879. ", pass 2");
  880. }
  881. bitrate = get_bit_rate(enc);
  882. if (bitrate != 0) {
  883. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  884. ", %d kb/s", bitrate / 1000);
  885. }
  886. }
  887. const char *av_get_profile_name(const AVCodec *codec, int profile)
  888. {
  889. const AVProfile *p;
  890. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  891. return NULL;
  892. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  893. if (p->profile == profile)
  894. return p->name;
  895. return NULL;
  896. }
  897. unsigned avcodec_version( void )
  898. {
  899. return LIBAVCODEC_VERSION_INT;
  900. }
  901. const char *avcodec_configuration(void)
  902. {
  903. return LIBAV_CONFIGURATION;
  904. }
  905. const char *avcodec_license(void)
  906. {
  907. #define LICENSE_PREFIX "libavcodec license: "
  908. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  909. }
  910. void avcodec_init(void)
  911. {
  912. static int initialized = 0;
  913. if (initialized != 0)
  914. return;
  915. initialized = 1;
  916. dsputil_static_init();
  917. }
  918. void avcodec_flush_buffers(AVCodecContext *avctx)
  919. {
  920. if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  921. ff_thread_flush(avctx);
  922. if(avctx->codec->flush)
  923. avctx->codec->flush(avctx);
  924. }
  925. void avcodec_default_free_buffers(AVCodecContext *s){
  926. int i, j;
  927. if(s->internal_buffer==NULL) return;
  928. if (s->internal_buffer_count)
  929. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  930. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  931. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  932. for(j=0; j<4; j++){
  933. av_freep(&buf->base[j]);
  934. buf->data[j]= NULL;
  935. }
  936. }
  937. av_freep(&s->internal_buffer);
  938. s->internal_buffer_count=0;
  939. }
  940. #if FF_API_OLD_FF_PICT_TYPES
  941. char av_get_pict_type_char(int pict_type){
  942. return av_get_picture_type_char(pict_type);
  943. }
  944. #endif
  945. int av_get_bits_per_sample(enum CodecID codec_id){
  946. switch(codec_id){
  947. case CODEC_ID_ADPCM_SBPRO_2:
  948. return 2;
  949. case CODEC_ID_ADPCM_SBPRO_3:
  950. return 3;
  951. case CODEC_ID_ADPCM_SBPRO_4:
  952. case CODEC_ID_ADPCM_CT:
  953. case CODEC_ID_ADPCM_IMA_WAV:
  954. case CODEC_ID_ADPCM_MS:
  955. case CODEC_ID_ADPCM_YAMAHA:
  956. return 4;
  957. case CODEC_ID_ADPCM_G722:
  958. case CODEC_ID_PCM_ALAW:
  959. case CODEC_ID_PCM_MULAW:
  960. case CODEC_ID_PCM_S8:
  961. case CODEC_ID_PCM_U8:
  962. case CODEC_ID_PCM_ZORK:
  963. return 8;
  964. case CODEC_ID_PCM_S16BE:
  965. case CODEC_ID_PCM_S16LE:
  966. case CODEC_ID_PCM_S16LE_PLANAR:
  967. case CODEC_ID_PCM_U16BE:
  968. case CODEC_ID_PCM_U16LE:
  969. return 16;
  970. case CODEC_ID_PCM_S24DAUD:
  971. case CODEC_ID_PCM_S24BE:
  972. case CODEC_ID_PCM_S24LE:
  973. case CODEC_ID_PCM_U24BE:
  974. case CODEC_ID_PCM_U24LE:
  975. return 24;
  976. case CODEC_ID_PCM_S32BE:
  977. case CODEC_ID_PCM_S32LE:
  978. case CODEC_ID_PCM_U32BE:
  979. case CODEC_ID_PCM_U32LE:
  980. case CODEC_ID_PCM_F32BE:
  981. case CODEC_ID_PCM_F32LE:
  982. return 32;
  983. case CODEC_ID_PCM_F64BE:
  984. case CODEC_ID_PCM_F64LE:
  985. return 64;
  986. default:
  987. return 0;
  988. }
  989. }
  990. #if FF_API_OLD_SAMPLE_FMT
  991. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  992. return av_get_bits_per_sample_fmt(sample_fmt);
  993. }
  994. #endif
  995. #if !HAVE_THREADS
  996. int ff_thread_init(AVCodecContext *s){
  997. return -1;
  998. }
  999. #endif
  1000. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1001. {
  1002. unsigned int n = 0;
  1003. while(v >= 0xff) {
  1004. *s++ = 0xff;
  1005. v -= 0xff;
  1006. n++;
  1007. }
  1008. *s = v;
  1009. n++;
  1010. return n;
  1011. }
  1012. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1013. int i;
  1014. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1015. return i;
  1016. }
  1017. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1018. {
  1019. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
  1020. "version to the newest one from Git. If the problem still "
  1021. "occurs, it means that your file has a feature which has not "
  1022. "been implemented.\n", feature);
  1023. if(want_sample)
  1024. av_log_ask_for_sample(avc, NULL);
  1025. }
  1026. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1027. {
  1028. va_list argument_list;
  1029. va_start(argument_list, msg);
  1030. if (msg)
  1031. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1032. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1033. "of this file to ftp://upload.libav.org/incoming/ "
  1034. "and contact the libav-devel mailing list.\n");
  1035. va_end(argument_list);
  1036. }
  1037. static AVHWAccel *first_hwaccel = NULL;
  1038. void av_register_hwaccel(AVHWAccel *hwaccel)
  1039. {
  1040. AVHWAccel **p = &first_hwaccel;
  1041. while (*p)
  1042. p = &(*p)->next;
  1043. *p = hwaccel;
  1044. hwaccel->next = NULL;
  1045. }
  1046. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1047. {
  1048. return hwaccel ? hwaccel->next : first_hwaccel;
  1049. }
  1050. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1051. {
  1052. AVHWAccel *hwaccel=NULL;
  1053. while((hwaccel= av_hwaccel_next(hwaccel))){
  1054. if ( hwaccel->id == codec_id
  1055. && hwaccel->pix_fmt == pix_fmt)
  1056. return hwaccel;
  1057. }
  1058. return NULL;
  1059. }
  1060. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1061. {
  1062. if (ff_lockmgr_cb) {
  1063. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1064. return -1;
  1065. }
  1066. ff_lockmgr_cb = cb;
  1067. if (ff_lockmgr_cb) {
  1068. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1069. return -1;
  1070. }
  1071. return 0;
  1072. }
  1073. unsigned int ff_toupper4(unsigned int x)
  1074. {
  1075. return toupper( x &0xFF)
  1076. + (toupper((x>>8 )&0xFF)<<8 )
  1077. + (toupper((x>>16)&0xFF)<<16)
  1078. + (toupper((x>>24)&0xFF)<<24);
  1079. }
  1080. #if !HAVE_PTHREADS
  1081. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1082. {
  1083. f->owner = avctx;
  1084. return avctx->get_buffer(avctx, f);
  1085. }
  1086. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1087. {
  1088. f->owner->release_buffer(f->owner, f);
  1089. }
  1090. void ff_thread_finish_setup(AVCodecContext *avctx)
  1091. {
  1092. }
  1093. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1094. {
  1095. }
  1096. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1097. {
  1098. }
  1099. #endif
  1100. #if FF_API_THREAD_INIT
  1101. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1102. {
  1103. s->thread_count = thread_count;
  1104. return ff_thread_init(s);
  1105. }
  1106. #endif