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.

1262 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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file libavcodec/utils.c
  24. * utils.
  25. */
  26. /* needed for mkstemp() */
  27. #define _XOPEN_SOURCE 600
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/integer.h"
  30. #include "libavutil/crc.h"
  31. #include "avcodec.h"
  32. #include "dsputil.h"
  33. #include "opt.h"
  34. #include "imgconvert.h"
  35. #include "audioconvert.h"
  36. #include "internal.h"
  37. #include <stdlib.h>
  38. #include <stdarg.h>
  39. #include <limits.h>
  40. #include <float.h>
  41. #if !HAVE_MKSTEMP
  42. #include <fcntl.h>
  43. #endif
  44. static int volatile entangled_thread_counter=0;
  45. int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  46. static void *codec_mutex;
  47. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  48. {
  49. if(min_size < *size)
  50. return ptr;
  51. *size= FFMAX(17*min_size/16 + 32, min_size);
  52. ptr= av_realloc(ptr, *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. *size= 0;
  55. return ptr;
  56. }
  57. void av_fast_malloc(void *ptr, unsigned int *size, unsigned int min_size)
  58. {
  59. void **p = ptr;
  60. if (min_size < *size)
  61. return;
  62. *size= FFMAX(17*min_size/16 + 32, min_size);
  63. av_free(*p);
  64. *p = av_malloc(*size);
  65. if (!*p) *size = 0;
  66. }
  67. /* encoder management */
  68. static AVCodec *first_avcodec = NULL;
  69. AVCodec *av_codec_next(AVCodec *c){
  70. if(c) return c->next;
  71. else return first_avcodec;
  72. }
  73. void avcodec_register(AVCodec *codec)
  74. {
  75. AVCodec **p;
  76. avcodec_init();
  77. p = &first_avcodec;
  78. while (*p != NULL) p = &(*p)->next;
  79. *p = codec;
  80. codec->next = NULL;
  81. }
  82. #if LIBAVCODEC_VERSION_MAJOR < 53
  83. void register_avcodec(AVCodec *codec)
  84. {
  85. avcodec_register(codec);
  86. }
  87. #endif
  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_dimensions(AVCodecContext *s, int *width, int *height){
  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)
  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)
  163. *height+=2; // some of the optimized chroma MC reads one line too much
  164. }
  165. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  166. if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
  167. return 0;
  168. av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
  169. return -1;
  170. }
  171. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  172. int i;
  173. int w= s->width;
  174. int h= s->height;
  175. InternalBuffer *buf;
  176. int *picture_number;
  177. if(pic->data[0]!=NULL) {
  178. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  179. return -1;
  180. }
  181. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  182. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  183. return -1;
  184. }
  185. if(avcodec_check_dimensions(s,w,h))
  186. return -1;
  187. if(s->internal_buffer==NULL){
  188. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  189. }
  190. #if 0
  191. s->internal_buffer= av_fast_realloc(
  192. s->internal_buffer,
  193. &s->internal_buffer_size,
  194. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  195. );
  196. #endif
  197. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  198. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  199. (*picture_number)++;
  200. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  201. for(i=0; i<4; i++){
  202. av_freep(&buf->base[i]);
  203. buf->data[i]= NULL;
  204. }
  205. }
  206. if(buf->base[0]){
  207. pic->age= *picture_number - buf->last_pic_num;
  208. buf->last_pic_num= *picture_number;
  209. }else{
  210. int h_chroma_shift, v_chroma_shift;
  211. int size[4] = {0};
  212. int tmpsize;
  213. int unaligned;
  214. AVPicture picture;
  215. int stride_align[4];
  216. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  217. avcodec_align_dimensions(s, &w, &h);
  218. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  219. w+= EDGE_WIDTH*2;
  220. h+= EDGE_WIDTH*2;
  221. }
  222. do {
  223. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  224. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  225. ff_fill_linesize(&picture, s->pix_fmt, w);
  226. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  227. w += w & ~(w-1);
  228. unaligned = 0;
  229. for (i=0; i<4; i++){
  230. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  231. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  232. //picture size unneccessarily in some cases. The solution here is not
  233. //pretty and better ideas are welcome!
  234. #if HAVE_MMX
  235. if(s->codec_id == CODEC_ID_SVQ1)
  236. stride_align[i]= 16;
  237. else
  238. #endif
  239. stride_align[i] = STRIDE_ALIGN;
  240. unaligned |= picture.linesize[i] % stride_align[i];
  241. }
  242. } while (unaligned);
  243. tmpsize = ff_fill_pointer(&picture, NULL, s->pix_fmt, h);
  244. if (tmpsize < 0)
  245. return -1;
  246. for (i=0; i<3 && picture.data[i+1]; i++)
  247. size[i] = picture.data[i+1] - picture.data[i];
  248. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  249. buf->last_pic_num= -256*256*256*64;
  250. memset(buf->base, 0, sizeof(buf->base));
  251. memset(buf->data, 0, sizeof(buf->data));
  252. for(i=0; i<4 && size[i]; i++){
  253. const int h_shift= i==0 ? 0 : h_chroma_shift;
  254. const int v_shift= i==0 ? 0 : v_chroma_shift;
  255. buf->linesize[i]= picture.linesize[i];
  256. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  257. if(buf->base[i]==NULL) return -1;
  258. memset(buf->base[i], 128, size[i]);
  259. // no edge if EDEG EMU or not planar YUV
  260. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  261. buf->data[i] = buf->base[i];
  262. else
  263. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  264. }
  265. if(size[1] && !size[2])
  266. ff_set_systematic_pal((uint32_t*)buf->data[1], s->pix_fmt);
  267. buf->width = s->width;
  268. buf->height = s->height;
  269. buf->pix_fmt= s->pix_fmt;
  270. pic->age= 256*256*256*64;
  271. }
  272. pic->type= FF_BUFFER_TYPE_INTERNAL;
  273. for(i=0; i<4; i++){
  274. pic->base[i]= buf->base[i];
  275. pic->data[i]= buf->data[i];
  276. pic->linesize[i]= buf->linesize[i];
  277. }
  278. s->internal_buffer_count++;
  279. pic->reordered_opaque= s->reordered_opaque;
  280. if(s->debug&FF_DEBUG_BUFFERS)
  281. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  282. return 0;
  283. }
  284. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  285. int i;
  286. InternalBuffer *buf, *last;
  287. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  288. assert(s->internal_buffer_count);
  289. buf = NULL; /* avoids warning */
  290. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  291. buf= &((InternalBuffer*)s->internal_buffer)[i];
  292. if(buf->data[0] == pic->data[0])
  293. break;
  294. }
  295. assert(i < s->internal_buffer_count);
  296. s->internal_buffer_count--;
  297. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  298. FFSWAP(InternalBuffer, *buf, *last);
  299. for(i=0; i<4; i++){
  300. pic->data[i]=NULL;
  301. // pic->base[i]=NULL;
  302. }
  303. //printf("R%X\n", pic->opaque);
  304. if(s->debug&FF_DEBUG_BUFFERS)
  305. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  306. }
  307. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  308. AVFrame temp_pic;
  309. int i;
  310. /* If no picture return a new buffer */
  311. if(pic->data[0] == NULL) {
  312. /* We will copy from buffer, so must be readable */
  313. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  314. return s->get_buffer(s, pic);
  315. }
  316. /* If internal buffer type return the same buffer */
  317. if(pic->type == FF_BUFFER_TYPE_INTERNAL)
  318. return 0;
  319. /*
  320. * Not internal type and reget_buffer not overridden, emulate cr buffer
  321. */
  322. temp_pic = *pic;
  323. for(i = 0; i < 4; i++)
  324. pic->data[i] = pic->base[i] = NULL;
  325. pic->opaque = NULL;
  326. /* Allocate new frame */
  327. if (s->get_buffer(s, pic))
  328. return -1;
  329. /* Copy image data from old buffer to new buffer */
  330. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  331. s->height);
  332. s->release_buffer(s, &temp_pic); // Release old frame
  333. return 0;
  334. }
  335. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  336. int i;
  337. for(i=0; i<count; i++){
  338. int r= func(c, (char*)arg + i*size);
  339. if(ret) ret[i]= r;
  340. }
  341. return 0;
  342. }
  343. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  344. int i;
  345. for(i=0; i<count; i++){
  346. int r= func(c, arg, i, 0);
  347. if(ret) ret[i]= r;
  348. }
  349. return 0;
  350. }
  351. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  352. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  353. ++fmt;
  354. return fmt[0];
  355. }
  356. void avcodec_get_frame_defaults(AVFrame *pic){
  357. memset(pic, 0, sizeof(AVFrame));
  358. pic->pts= AV_NOPTS_VALUE;
  359. pic->key_frame= 1;
  360. }
  361. AVFrame *avcodec_alloc_frame(void){
  362. AVFrame *pic= av_malloc(sizeof(AVFrame));
  363. if(pic==NULL) return NULL;
  364. avcodec_get_frame_defaults(pic);
  365. return pic;
  366. }
  367. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  368. {
  369. int ret= -1;
  370. /* If there is a user-supplied mutex locking routine, call it. */
  371. if (ff_lockmgr_cb) {
  372. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  373. return -1;
  374. }
  375. entangled_thread_counter++;
  376. if(entangled_thread_counter != 1){
  377. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  378. goto end;
  379. }
  380. if(avctx->codec || !codec)
  381. goto end;
  382. if (codec->priv_data_size > 0) {
  383. avctx->priv_data = av_mallocz(codec->priv_data_size);
  384. if (!avctx->priv_data) {
  385. ret = AVERROR(ENOMEM);
  386. goto end;
  387. }
  388. } else {
  389. avctx->priv_data = NULL;
  390. }
  391. if(avctx->coded_width && avctx->coded_height)
  392. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  393. else if(avctx->width && avctx->height)
  394. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  395. #define SANE_NB_CHANNELS 128U
  396. if (((avctx->coded_width || avctx->coded_height)
  397. && avcodec_check_dimensions(avctx, avctx->coded_width, avctx->coded_height))
  398. || avctx->channels > SANE_NB_CHANNELS) {
  399. ret = AVERROR(EINVAL);
  400. goto free_and_end;
  401. }
  402. avctx->codec = codec;
  403. if ((avctx->codec_type == CODEC_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  404. avctx->codec_id == CODEC_ID_NONE) {
  405. avctx->codec_type = codec->type;
  406. avctx->codec_id = codec->id;
  407. }
  408. if(avctx->codec_id != codec->id || avctx->codec_type != codec->type){
  409. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  410. goto free_and_end;
  411. }
  412. avctx->frame_number = 0;
  413. if(avctx->codec->init){
  414. ret = avctx->codec->init(avctx);
  415. if (ret < 0) {
  416. goto free_and_end;
  417. }
  418. }
  419. ret=0;
  420. end:
  421. entangled_thread_counter--;
  422. /* Release any user-supplied mutex. */
  423. if (ff_lockmgr_cb) {
  424. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  425. }
  426. return ret;
  427. free_and_end:
  428. av_freep(&avctx->priv_data);
  429. avctx->codec= NULL;
  430. goto end;
  431. }
  432. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  433. const short *samples)
  434. {
  435. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  436. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  437. return -1;
  438. }
  439. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  440. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  441. avctx->frame_number++;
  442. return ret;
  443. }else
  444. return 0;
  445. }
  446. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  447. const AVFrame *pict)
  448. {
  449. if(buf_size < FF_MIN_BUFFER_SIZE){
  450. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  451. return -1;
  452. }
  453. if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
  454. return -1;
  455. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  456. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  457. avctx->frame_number++;
  458. emms_c(); //needed to avoid an emms_c() call before every return;
  459. return ret;
  460. }else
  461. return 0;
  462. }
  463. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  464. const AVSubtitle *sub)
  465. {
  466. int ret;
  467. if(sub->start_display_time) {
  468. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  469. return -1;
  470. }
  471. if(sub->num_rects == 0 || !sub->rects)
  472. return -1;
  473. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  474. avctx->frame_number++;
  475. return ret;
  476. }
  477. #if LIBAVCODEC_VERSION_MAJOR < 53
  478. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  479. int *got_picture_ptr,
  480. const uint8_t *buf, int buf_size)
  481. {
  482. AVPacket avpkt;
  483. av_init_packet(&avpkt);
  484. avpkt.data = buf;
  485. avpkt.size = buf_size;
  486. // HACK for CorePNG to decode as normal PNG by default
  487. avpkt.flags = AV_PKT_FLAG_KEY;
  488. return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
  489. }
  490. #endif
  491. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  492. int *got_picture_ptr,
  493. AVPacket *avpkt)
  494. {
  495. int ret;
  496. *got_picture_ptr= 0;
  497. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
  498. return -1;
  499. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  500. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  501. avpkt);
  502. emms_c(); //needed to avoid an emms_c() call before every return;
  503. if (*got_picture_ptr)
  504. avctx->frame_number++;
  505. }else
  506. ret= 0;
  507. return ret;
  508. }
  509. #if LIBAVCODEC_VERSION_MAJOR < 53
  510. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  511. int *frame_size_ptr,
  512. const uint8_t *buf, int buf_size)
  513. {
  514. AVPacket avpkt;
  515. av_init_packet(&avpkt);
  516. avpkt.data = buf;
  517. avpkt.size = buf_size;
  518. return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
  519. }
  520. #endif
  521. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  522. int *frame_size_ptr,
  523. AVPacket *avpkt)
  524. {
  525. int ret;
  526. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  527. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  528. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  529. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  530. return -1;
  531. }
  532. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  533. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  534. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  535. return -1;
  536. }
  537. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  538. avctx->frame_number++;
  539. }else{
  540. ret= 0;
  541. *frame_size_ptr=0;
  542. }
  543. return ret;
  544. }
  545. #if LIBAVCODEC_VERSION_MAJOR < 53
  546. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  547. int *got_sub_ptr,
  548. const uint8_t *buf, int buf_size)
  549. {
  550. AVPacket avpkt;
  551. av_init_packet(&avpkt);
  552. avpkt.data = buf;
  553. avpkt.size = buf_size;
  554. return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
  555. }
  556. #endif
  557. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  558. int *got_sub_ptr,
  559. AVPacket *avpkt)
  560. {
  561. int ret;
  562. *got_sub_ptr = 0;
  563. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  564. if (*got_sub_ptr)
  565. avctx->frame_number++;
  566. return ret;
  567. }
  568. int avcodec_close(AVCodecContext *avctx)
  569. {
  570. /* If there is a user-supplied mutex locking routine, call it. */
  571. if (ff_lockmgr_cb) {
  572. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  573. return -1;
  574. }
  575. entangled_thread_counter++;
  576. if(entangled_thread_counter != 1){
  577. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  578. entangled_thread_counter--;
  579. return -1;
  580. }
  581. if (HAVE_THREADS && avctx->thread_opaque)
  582. avcodec_thread_free(avctx);
  583. if (avctx->codec && avctx->codec->close)
  584. avctx->codec->close(avctx);
  585. avcodec_default_free_buffers(avctx);
  586. av_freep(&avctx->priv_data);
  587. avctx->codec = NULL;
  588. entangled_thread_counter--;
  589. /* Release any user-supplied mutex. */
  590. if (ff_lockmgr_cb) {
  591. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  592. }
  593. return 0;
  594. }
  595. AVCodec *avcodec_find_encoder(enum CodecID id)
  596. {
  597. AVCodec *p;
  598. p = first_avcodec;
  599. while (p) {
  600. if (p->encode != NULL && p->id == id)
  601. return p;
  602. p = p->next;
  603. }
  604. return NULL;
  605. }
  606. AVCodec *avcodec_find_encoder_by_name(const char *name)
  607. {
  608. AVCodec *p;
  609. if (!name)
  610. return NULL;
  611. p = first_avcodec;
  612. while (p) {
  613. if (p->encode != NULL && strcmp(name,p->name) == 0)
  614. return p;
  615. p = p->next;
  616. }
  617. return NULL;
  618. }
  619. AVCodec *avcodec_find_decoder(enum CodecID id)
  620. {
  621. AVCodec *p;
  622. p = first_avcodec;
  623. while (p) {
  624. if (p->decode != NULL && p->id == id)
  625. return p;
  626. p = p->next;
  627. }
  628. return NULL;
  629. }
  630. AVCodec *avcodec_find_decoder_by_name(const char *name)
  631. {
  632. AVCodec *p;
  633. if (!name)
  634. return NULL;
  635. p = first_avcodec;
  636. while (p) {
  637. if (p->decode != NULL && strcmp(name,p->name) == 0)
  638. return p;
  639. p = p->next;
  640. }
  641. return NULL;
  642. }
  643. int av_get_bit_rate(AVCodecContext *ctx)
  644. {
  645. int bit_rate;
  646. int bits_per_sample;
  647. switch(ctx->codec_type) {
  648. case CODEC_TYPE_VIDEO:
  649. bit_rate = ctx->bit_rate;
  650. break;
  651. case CODEC_TYPE_AUDIO:
  652. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  653. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  654. break;
  655. case CODEC_TYPE_DATA:
  656. bit_rate = ctx->bit_rate;
  657. break;
  658. case CODEC_TYPE_SUBTITLE:
  659. bit_rate = ctx->bit_rate;
  660. break;
  661. case CODEC_TYPE_ATTACHMENT:
  662. bit_rate = ctx->bit_rate;
  663. break;
  664. default:
  665. bit_rate = 0;
  666. break;
  667. }
  668. return bit_rate;
  669. }
  670. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  671. {
  672. const char *codec_name;
  673. AVCodec *p;
  674. char buf1[32];
  675. int bitrate;
  676. AVRational display_aspect_ratio;
  677. if (encode)
  678. p = avcodec_find_encoder(enc->codec_id);
  679. else
  680. p = avcodec_find_decoder(enc->codec_id);
  681. if (p) {
  682. codec_name = p->name;
  683. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  684. /* fake mpeg2 transport stream codec (currently not
  685. registered) */
  686. codec_name = "mpeg2ts";
  687. } else if (enc->codec_name[0] != '\0') {
  688. codec_name = enc->codec_name;
  689. } else {
  690. /* output avi tags */
  691. if( isprint(enc->codec_tag&0xFF) && isprint((enc->codec_tag>>8)&0xFF)
  692. && isprint((enc->codec_tag>>16)&0xFF) && isprint((enc->codec_tag>>24)&0xFF)){
  693. snprintf(buf1, sizeof(buf1), "%c%c%c%c / 0x%04X",
  694. enc->codec_tag & 0xff,
  695. (enc->codec_tag >> 8) & 0xff,
  696. (enc->codec_tag >> 16) & 0xff,
  697. (enc->codec_tag >> 24) & 0xff,
  698. enc->codec_tag);
  699. } else {
  700. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  701. }
  702. codec_name = buf1;
  703. }
  704. switch(enc->codec_type) {
  705. case CODEC_TYPE_VIDEO:
  706. snprintf(buf, buf_size,
  707. "Video: %s%s",
  708. codec_name, enc->mb_decision ? " (hq)" : "");
  709. if (enc->pix_fmt != PIX_FMT_NONE) {
  710. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  711. ", %s",
  712. avcodec_get_pix_fmt_name(enc->pix_fmt));
  713. }
  714. if (enc->width) {
  715. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  716. ", %dx%d",
  717. enc->width, enc->height);
  718. if (enc->sample_aspect_ratio.num) {
  719. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  720. enc->width*enc->sample_aspect_ratio.num,
  721. enc->height*enc->sample_aspect_ratio.den,
  722. 1024*1024);
  723. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  724. " [PAR %d:%d DAR %d:%d]",
  725. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  726. display_aspect_ratio.num, display_aspect_ratio.den);
  727. }
  728. if(av_log_get_level() >= AV_LOG_DEBUG){
  729. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  730. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  731. ", %d/%d",
  732. enc->time_base.num/g, enc->time_base.den/g);
  733. }
  734. }
  735. if (encode) {
  736. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  737. ", q=%d-%d", enc->qmin, enc->qmax);
  738. }
  739. break;
  740. case CODEC_TYPE_AUDIO:
  741. snprintf(buf, buf_size,
  742. "Audio: %s",
  743. codec_name);
  744. if (enc->sample_rate) {
  745. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  746. ", %d Hz", enc->sample_rate);
  747. }
  748. av_strlcat(buf, ", ", buf_size);
  749. avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  750. if (enc->sample_fmt != SAMPLE_FMT_NONE) {
  751. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  752. ", %s", avcodec_get_sample_fmt_name(enc->sample_fmt));
  753. }
  754. break;
  755. case CODEC_TYPE_DATA:
  756. snprintf(buf, buf_size, "Data: %s", codec_name);
  757. break;
  758. case CODEC_TYPE_SUBTITLE:
  759. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  760. break;
  761. case CODEC_TYPE_ATTACHMENT:
  762. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  763. break;
  764. default:
  765. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  766. return;
  767. }
  768. if (encode) {
  769. if (enc->flags & CODEC_FLAG_PASS1)
  770. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  771. ", pass 1");
  772. if (enc->flags & CODEC_FLAG_PASS2)
  773. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  774. ", pass 2");
  775. }
  776. bitrate = av_get_bit_rate(enc);
  777. if (bitrate != 0) {
  778. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  779. ", %d kb/s", bitrate / 1000);
  780. }
  781. }
  782. unsigned avcodec_version( void )
  783. {
  784. return LIBAVCODEC_VERSION_INT;
  785. }
  786. const char * avcodec_configuration(void)
  787. {
  788. return FFMPEG_CONFIGURATION;
  789. }
  790. const char * avcodec_license(void)
  791. {
  792. #define LICENSE_PREFIX "libavcodec license: "
  793. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  794. }
  795. void avcodec_init(void)
  796. {
  797. static int initialized = 0;
  798. if (initialized != 0)
  799. return;
  800. initialized = 1;
  801. dsputil_static_init();
  802. }
  803. void avcodec_flush_buffers(AVCodecContext *avctx)
  804. {
  805. if(avctx->codec->flush)
  806. avctx->codec->flush(avctx);
  807. }
  808. void avcodec_default_free_buffers(AVCodecContext *s){
  809. int i, j;
  810. if(s->internal_buffer==NULL) return;
  811. if (s->internal_buffer_count)
  812. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  813. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  814. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  815. for(j=0; j<4; j++){
  816. av_freep(&buf->base[j]);
  817. buf->data[j]= NULL;
  818. }
  819. }
  820. av_freep(&s->internal_buffer);
  821. s->internal_buffer_count=0;
  822. }
  823. char av_get_pict_type_char(int pict_type){
  824. switch(pict_type){
  825. case FF_I_TYPE: return 'I';
  826. case FF_P_TYPE: return 'P';
  827. case FF_B_TYPE: return 'B';
  828. case FF_S_TYPE: return 'S';
  829. case FF_SI_TYPE:return 'i';
  830. case FF_SP_TYPE:return 'p';
  831. case FF_BI_TYPE:return 'b';
  832. default: return '?';
  833. }
  834. }
  835. int av_get_bits_per_sample(enum CodecID codec_id){
  836. switch(codec_id){
  837. case CODEC_ID_ADPCM_SBPRO_2:
  838. return 2;
  839. case CODEC_ID_ADPCM_SBPRO_3:
  840. return 3;
  841. case CODEC_ID_ADPCM_SBPRO_4:
  842. case CODEC_ID_ADPCM_CT:
  843. return 4;
  844. case CODEC_ID_PCM_ALAW:
  845. case CODEC_ID_PCM_MULAW:
  846. case CODEC_ID_PCM_S8:
  847. case CODEC_ID_PCM_U8:
  848. case CODEC_ID_PCM_ZORK:
  849. return 8;
  850. case CODEC_ID_PCM_S16BE:
  851. case CODEC_ID_PCM_S16LE:
  852. case CODEC_ID_PCM_S16LE_PLANAR:
  853. case CODEC_ID_PCM_U16BE:
  854. case CODEC_ID_PCM_U16LE:
  855. return 16;
  856. case CODEC_ID_PCM_S24DAUD:
  857. case CODEC_ID_PCM_S24BE:
  858. case CODEC_ID_PCM_S24LE:
  859. case CODEC_ID_PCM_U24BE:
  860. case CODEC_ID_PCM_U24LE:
  861. return 24;
  862. case CODEC_ID_PCM_S32BE:
  863. case CODEC_ID_PCM_S32LE:
  864. case CODEC_ID_PCM_U32BE:
  865. case CODEC_ID_PCM_U32LE:
  866. case CODEC_ID_PCM_F32BE:
  867. case CODEC_ID_PCM_F32LE:
  868. return 32;
  869. case CODEC_ID_PCM_F64BE:
  870. case CODEC_ID_PCM_F64LE:
  871. return 64;
  872. default:
  873. return 0;
  874. }
  875. }
  876. int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
  877. switch (sample_fmt) {
  878. case SAMPLE_FMT_U8:
  879. return 8;
  880. case SAMPLE_FMT_S16:
  881. return 16;
  882. case SAMPLE_FMT_S32:
  883. case SAMPLE_FMT_FLT:
  884. return 32;
  885. case SAMPLE_FMT_DBL:
  886. return 64;
  887. default:
  888. return 0;
  889. }
  890. }
  891. #if !HAVE_THREADS
  892. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  893. s->thread_count = thread_count;
  894. return -1;
  895. }
  896. #endif
  897. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  898. {
  899. unsigned int n = 0;
  900. while(v >= 0xff) {
  901. *s++ = 0xff;
  902. v -= 0xff;
  903. n++;
  904. }
  905. *s = v;
  906. n++;
  907. return n;
  908. }
  909. /* Wrapper to work around the lack of mkstemp() on mingw/cygin.
  910. * Also, tries to create file in /tmp first, if possible.
  911. * *prefix can be a character constant; *filename will be allocated internally.
  912. * Returns file descriptor of opened file (or -1 on error)
  913. * and opened file name in **filename. */
  914. int av_tempfile(char *prefix, char **filename) {
  915. int fd=-1;
  916. #if !HAVE_MKSTEMP
  917. *filename = tempnam(".", prefix);
  918. #else
  919. size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
  920. *filename = av_malloc(len);
  921. #endif
  922. /* -----common section-----*/
  923. if (*filename == NULL) {
  924. av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
  925. return -1;
  926. }
  927. #if !HAVE_MKSTEMP
  928. fd = open(*filename, O_RDWR | O_BINARY | O_CREAT, 0444);
  929. #else
  930. snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
  931. fd = mkstemp(*filename);
  932. if (fd < 0) {
  933. snprintf(*filename, len, "./%sXXXXXX", prefix);
  934. fd = mkstemp(*filename);
  935. }
  936. #endif
  937. /* -----common section-----*/
  938. if (fd < 0) {
  939. av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
  940. return -1;
  941. }
  942. return fd; /* success */
  943. }
  944. typedef struct {
  945. const char *abbr;
  946. int width, height;
  947. } VideoFrameSizeAbbr;
  948. typedef struct {
  949. const char *abbr;
  950. int rate_num, rate_den;
  951. } VideoFrameRateAbbr;
  952. static const VideoFrameSizeAbbr video_frame_size_abbrs[] = {
  953. { "ntsc", 720, 480 },
  954. { "pal", 720, 576 },
  955. { "qntsc", 352, 240 }, /* VCD compliant NTSC */
  956. { "qpal", 352, 288 }, /* VCD compliant PAL */
  957. { "sntsc", 640, 480 }, /* square pixel NTSC */
  958. { "spal", 768, 576 }, /* square pixel PAL */
  959. { "film", 352, 240 },
  960. { "ntsc-film", 352, 240 },
  961. { "sqcif", 128, 96 },
  962. { "qcif", 176, 144 },
  963. { "cif", 352, 288 },
  964. { "4cif", 704, 576 },
  965. { "16cif", 1408,1152 },
  966. { "qqvga", 160, 120 },
  967. { "qvga", 320, 240 },
  968. { "vga", 640, 480 },
  969. { "svga", 800, 600 },
  970. { "xga", 1024, 768 },
  971. { "uxga", 1600,1200 },
  972. { "qxga", 2048,1536 },
  973. { "sxga", 1280,1024 },
  974. { "qsxga", 2560,2048 },
  975. { "hsxga", 5120,4096 },
  976. { "wvga", 852, 480 },
  977. { "wxga", 1366, 768 },
  978. { "wsxga", 1600,1024 },
  979. { "wuxga", 1920,1200 },
  980. { "woxga", 2560,1600 },
  981. { "wqsxga", 3200,2048 },
  982. { "wquxga", 3840,2400 },
  983. { "whsxga", 6400,4096 },
  984. { "whuxga", 7680,4800 },
  985. { "cga", 320, 200 },
  986. { "ega", 640, 350 },
  987. { "hd480", 852, 480 },
  988. { "hd720", 1280, 720 },
  989. { "hd1080", 1920,1080 },
  990. };
  991. static const VideoFrameRateAbbr video_frame_rate_abbrs[]= {
  992. { "ntsc", 30000, 1001 },
  993. { "pal", 25, 1 },
  994. { "qntsc", 30000, 1001 }, /* VCD compliant NTSC */
  995. { "qpal", 25, 1 }, /* VCD compliant PAL */
  996. { "sntsc", 30000, 1001 }, /* square pixel NTSC */
  997. { "spal", 25, 1 }, /* square pixel PAL */
  998. { "film", 24, 1 },
  999. { "ntsc-film", 24000, 1001 },
  1000. };
  1001. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  1002. {
  1003. int i;
  1004. int n = FF_ARRAY_ELEMS(video_frame_size_abbrs);
  1005. char *p;
  1006. int frame_width = 0, frame_height = 0;
  1007. for(i=0;i<n;i++) {
  1008. if (!strcmp(video_frame_size_abbrs[i].abbr, str)) {
  1009. frame_width = video_frame_size_abbrs[i].width;
  1010. frame_height = video_frame_size_abbrs[i].height;
  1011. break;
  1012. }
  1013. }
  1014. if (i == n) {
  1015. p = str;
  1016. frame_width = strtol(p, &p, 10);
  1017. if (*p)
  1018. p++;
  1019. frame_height = strtol(p, &p, 10);
  1020. }
  1021. if (frame_width <= 0 || frame_height <= 0)
  1022. return -1;
  1023. *width_ptr = frame_width;
  1024. *height_ptr = frame_height;
  1025. return 0;
  1026. }
  1027. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  1028. {
  1029. int i;
  1030. int n = FF_ARRAY_ELEMS(video_frame_rate_abbrs);
  1031. char* cp;
  1032. /* First, we check our abbreviation table */
  1033. for (i = 0; i < n; ++i)
  1034. if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) {
  1035. frame_rate->num = video_frame_rate_abbrs[i].rate_num;
  1036. frame_rate->den = video_frame_rate_abbrs[i].rate_den;
  1037. return 0;
  1038. }
  1039. /* Then, we try to parse it as fraction */
  1040. cp = strchr(arg, '/');
  1041. if (!cp)
  1042. cp = strchr(arg, ':');
  1043. if (cp) {
  1044. char* cpp;
  1045. frame_rate->num = strtol(arg, &cpp, 10);
  1046. if (cpp != arg || cpp == cp)
  1047. frame_rate->den = strtol(cp+1, &cpp, 10);
  1048. else
  1049. frame_rate->num = 0;
  1050. }
  1051. else {
  1052. /* Finally we give up and parse it as double */
  1053. AVRational time_base = av_d2q(strtod(arg, 0), 1001000);
  1054. frame_rate->den = time_base.den;
  1055. frame_rate->num = time_base.num;
  1056. }
  1057. if (!frame_rate->num || !frame_rate->den)
  1058. return -1;
  1059. else
  1060. return 0;
  1061. }
  1062. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1063. {
  1064. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1065. "version to the newest one from SVN. If the problem still "
  1066. "occurs, it means that your file has a feature which has not "
  1067. "been implemented.", feature);
  1068. if(want_sample)
  1069. av_log_ask_for_sample(avc, NULL);
  1070. else
  1071. av_log(avc, AV_LOG_WARNING, "\n");
  1072. }
  1073. void av_log_ask_for_sample(void *avc, const char *msg)
  1074. {
  1075. if (msg)
  1076. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  1077. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1078. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1079. "and contact the ffmpeg-devel mailing list.\n");
  1080. }
  1081. static AVHWAccel *first_hwaccel = NULL;
  1082. void av_register_hwaccel(AVHWAccel *hwaccel)
  1083. {
  1084. AVHWAccel **p = &first_hwaccel;
  1085. while (*p)
  1086. p = &(*p)->next;
  1087. *p = hwaccel;
  1088. hwaccel->next = NULL;
  1089. }
  1090. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1091. {
  1092. return hwaccel ? hwaccel->next : first_hwaccel;
  1093. }
  1094. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1095. {
  1096. AVHWAccel *hwaccel=NULL;
  1097. while((hwaccel= av_hwaccel_next(hwaccel))){
  1098. if ( hwaccel->id == codec_id
  1099. && hwaccel->pix_fmt == pix_fmt)
  1100. return hwaccel;
  1101. }
  1102. return NULL;
  1103. }
  1104. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1105. {
  1106. if (ff_lockmgr_cb) {
  1107. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1108. return -1;
  1109. }
  1110. ff_lockmgr_cb = cb;
  1111. if (ff_lockmgr_cb) {
  1112. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1113. return -1;
  1114. }
  1115. return 0;
  1116. }