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.

1256 lines
36KB

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