rawdec.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Raw Video Decoder
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/rawdec.c
  23. * Raw Video Decoder
  24. */
  25. #include "avcodec.h"
  26. #include "raw.h"
  27. typedef struct RawVideoContext {
  28. unsigned char * buffer; /* block of memory for holding one frame */
  29. int length; /* number of bytes in buffer */
  30. int flip;
  31. AVFrame pic; ///< AVCodecContext.coded_frame
  32. } RawVideoContext;
  33. static const PixelFormatTag pixelFormatBpsAVI[] = {
  34. { PIX_FMT_PAL8, 4 },
  35. { PIX_FMT_PAL8, 8 },
  36. { PIX_FMT_RGB555, 15 },
  37. { PIX_FMT_RGB555, 16 },
  38. { PIX_FMT_BGR24, 24 },
  39. { PIX_FMT_RGB32, 32 },
  40. { PIX_FMT_NONE, 0 },
  41. };
  42. static const PixelFormatTag pixelFormatBpsMOV[] = {
  43. /* FIXME fix swscaler to support those */
  44. /* http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/chapter_4_section_2.html */
  45. { PIX_FMT_PAL8, 4 },
  46. { PIX_FMT_PAL8, 8 },
  47. { PIX_FMT_BGR555, 16 },
  48. { PIX_FMT_RGB24, 24 },
  49. { PIX_FMT_BGR32_1, 32 },
  50. { PIX_FMT_NONE, 0 },
  51. };
  52. static enum PixelFormat findPixelFormat(const PixelFormatTag *tags, unsigned int fourcc)
  53. {
  54. while (tags->pix_fmt >= 0) {
  55. if (tags->fourcc == fourcc)
  56. return tags->pix_fmt;
  57. tags++;
  58. }
  59. return PIX_FMT_YUV420P;
  60. }
  61. static av_cold int raw_init_decoder(AVCodecContext *avctx)
  62. {
  63. RawVideoContext *context = avctx->priv_data;
  64. if (avctx->codec_tag == MKTAG('r','a','w',' '))
  65. avctx->pix_fmt = findPixelFormat(pixelFormatBpsMOV, avctx->bits_per_coded_sample);
  66. else if (avctx->codec_tag)
  67. avctx->pix_fmt = findPixelFormat(ff_raw_pixelFormatTags, avctx->codec_tag);
  68. else if (avctx->bits_per_coded_sample)
  69. avctx->pix_fmt = findPixelFormat(pixelFormatBpsAVI, avctx->bits_per_coded_sample);
  70. context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  71. context->buffer = av_malloc(context->length);
  72. context->pic.pict_type = FF_I_TYPE;
  73. context->pic.key_frame = 1;
  74. avctx->coded_frame= &context->pic;
  75. if (!context->buffer)
  76. return -1;
  77. if(avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9))
  78. context->flip=1;
  79. return 0;
  80. }
  81. static void flip(AVCodecContext *avctx, AVPicture * picture){
  82. picture->data[0] += picture->linesize[0] * (avctx->height-1);
  83. picture->linesize[0] *= -1;
  84. }
  85. static int raw_decode(AVCodecContext *avctx,
  86. void *data, int *data_size,
  87. const uint8_t *buf, int buf_size)
  88. {
  89. RawVideoContext *context = avctx->priv_data;
  90. AVFrame * frame = (AVFrame *) data;
  91. AVPicture * picture = (AVPicture *) data;
  92. frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
  93. frame->top_field_first = avctx->coded_frame->top_field_first;
  94. //4bpp raw in avi and mov (yes this is ugly ...)
  95. if(avctx->bits_per_coded_sample == 4 && avctx->pix_fmt==PIX_FMT_PAL8 &&
  96. (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))){
  97. int i;
  98. for(i=256*2; i+1 < context->length>>1; i++){
  99. context->buffer[2*i+0]= buf[i-256*2]>>4;
  100. context->buffer[2*i+1]= buf[i-256*2]&15;
  101. }
  102. buf= context->buffer + 256*4;
  103. buf_size= context->length - 256*4;
  104. }
  105. if(buf_size < context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0))
  106. return -1;
  107. avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
  108. if(avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length){
  109. frame->data[1]= context->buffer;
  110. }
  111. if (avctx->palctrl && avctx->palctrl->palette_changed) {
  112. memcpy(frame->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
  113. avctx->palctrl->palette_changed = 0;
  114. }
  115. if(context->flip)
  116. flip(avctx, picture);
  117. if (avctx->codec_tag == MKTAG('Y', 'V', '1', '2'))
  118. {
  119. // swap fields
  120. unsigned char *tmp = picture->data[1];
  121. picture->data[1] = picture->data[2];
  122. picture->data[2] = tmp;
  123. }
  124. *data_size = sizeof(AVPicture);
  125. return buf_size;
  126. }
  127. static av_cold int raw_close_decoder(AVCodecContext *avctx)
  128. {
  129. RawVideoContext *context = avctx->priv_data;
  130. av_freep(&context->buffer);
  131. return 0;
  132. }
  133. AVCodec rawvideo_decoder = {
  134. "rawvideo",
  135. CODEC_TYPE_VIDEO,
  136. CODEC_ID_RAWVIDEO,
  137. sizeof(RawVideoContext),
  138. raw_init_decoder,
  139. NULL,
  140. raw_close_decoder,
  141. raw_decode,
  142. .long_name = NULL_IF_CONFIG_SMALL("raw video"),
  143. };