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.

207 lines
6.0KB

  1. /*
  2. * Raw Video Codec
  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 raw.c
  23. * Raw Video Codec
  24. */
  25. #include "avcodec.h"
  26. typedef struct RawVideoContext {
  27. unsigned char * buffer; /* block of memory for holding one frame */
  28. int length; /* number of bytes in buffer */
  29. AVFrame pic; ///< AVCodecContext.coded_frame
  30. } RawVideoContext;
  31. typedef struct PixelFormatTag {
  32. int pix_fmt;
  33. unsigned int fourcc;
  34. } PixelFormatTag;
  35. const PixelFormatTag pixelFormatTags[] = {
  36. { PIX_FMT_YUV420P, MKTAG('I', '4', '2', '0') }, /* Planar formats */
  37. { PIX_FMT_YUV420P, MKTAG('I', 'Y', 'U', 'V') },
  38. { PIX_FMT_YUV420P, MKTAG('Y', 'V', '1', '2') },
  39. { PIX_FMT_YUV410P, MKTAG('Y', 'U', 'V', '9') },
  40. { PIX_FMT_YUV411P, MKTAG('Y', '4', '1', 'B') },
  41. { PIX_FMT_YUV422P, MKTAG('Y', '4', '2', 'B') },
  42. { PIX_FMT_GRAY8, MKTAG('Y', '8', '0', '0') },
  43. { PIX_FMT_GRAY8, MKTAG(' ', ' ', 'Y', '8') },
  44. { PIX_FMT_YUV422, MKTAG('Y', 'U', 'Y', '2') }, /* Packed formats */
  45. { PIX_FMT_YUV422, MKTAG('Y', '4', '2', '2') },
  46. { PIX_FMT_UYVY422, MKTAG('U', 'Y', 'V', 'Y') },
  47. { PIX_FMT_GRAY8, MKTAG('G', 'R', 'E', 'Y') },
  48. /* quicktime */
  49. { PIX_FMT_UYVY422, MKTAG('2', 'v', 'u', 'y') },
  50. { -1, 0 },
  51. };
  52. static int findPixelFormat(unsigned int fourcc)
  53. {
  54. const PixelFormatTag * tags = pixelFormatTags;
  55. while (tags->pix_fmt >= 0) {
  56. if (tags->fourcc == fourcc)
  57. return tags->pix_fmt;
  58. tags++;
  59. }
  60. return PIX_FMT_YUV420P;
  61. }
  62. unsigned int avcodec_pix_fmt_to_codec_tag(enum PixelFormat fmt)
  63. {
  64. const PixelFormatTag * tags = pixelFormatTags;
  65. while (tags->pix_fmt >= 0) {
  66. if (tags->pix_fmt == fmt)
  67. return tags->fourcc;
  68. tags++;
  69. }
  70. return 0;
  71. }
  72. /* RAW Decoder Implementation */
  73. static int raw_init_decoder(AVCodecContext *avctx)
  74. {
  75. RawVideoContext *context = avctx->priv_data;
  76. if (avctx->codec_tag)
  77. avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
  78. else if (avctx->bits_per_sample){
  79. switch(avctx->bits_per_sample){
  80. case 8: avctx->pix_fmt= PIX_FMT_PAL8 ; break;
  81. case 15: avctx->pix_fmt= PIX_FMT_RGB555; break;
  82. case 16: avctx->pix_fmt= PIX_FMT_RGB565; break;
  83. case 24: avctx->pix_fmt= PIX_FMT_BGR24 ; break;
  84. case 32: avctx->pix_fmt= PIX_FMT_RGBA32; break;
  85. }
  86. }
  87. context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  88. context->buffer = av_malloc(context->length);
  89. context->pic.pict_type = FF_I_TYPE;
  90. context->pic.key_frame = 1;
  91. avctx->coded_frame= &context->pic;
  92. if (!context->buffer)
  93. return -1;
  94. return 0;
  95. }
  96. static void flip(AVCodecContext *avctx, AVPicture * picture){
  97. if(!avctx->codec_tag && avctx->bits_per_sample && picture->linesize[2]==0){
  98. picture->data[0] += picture->linesize[0] * (avctx->height-1);
  99. picture->linesize[0] *= -1;
  100. }
  101. }
  102. static int raw_decode(AVCodecContext *avctx,
  103. void *data, int *data_size,
  104. uint8_t *buf, int buf_size)
  105. {
  106. RawVideoContext *context = avctx->priv_data;
  107. AVFrame * frame = (AVFrame *) data;
  108. AVPicture * picture = (AVPicture *) data;
  109. frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
  110. frame->top_field_first = avctx->coded_frame->top_field_first;
  111. if(buf_size < context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0))
  112. return -1;
  113. avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
  114. if(avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length){
  115. frame->data[1]= context->buffer;
  116. }
  117. if (avctx->palctrl && avctx->palctrl->palette_changed) {
  118. memcpy(frame->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
  119. avctx->palctrl->palette_changed = 0;
  120. }
  121. flip(avctx, picture);
  122. if (avctx->codec_tag == MKTAG('Y', 'V', '1', '2'))
  123. {
  124. // swap fields
  125. unsigned char *tmp = picture->data[1];
  126. picture->data[1] = picture->data[2];
  127. picture->data[2] = tmp;
  128. }
  129. *data_size = sizeof(AVPicture);
  130. return buf_size;
  131. }
  132. static int raw_close_decoder(AVCodecContext *avctx)
  133. {
  134. RawVideoContext *context = avctx->priv_data;
  135. av_freep(&context->buffer);
  136. return 0;
  137. }
  138. /* RAW Encoder Implementation */
  139. #ifdef CONFIG_RAWVIDEO_ENCODER
  140. static int raw_init_encoder(AVCodecContext *avctx)
  141. {
  142. avctx->coded_frame = (AVFrame *)avctx->priv_data;
  143. avctx->coded_frame->pict_type = FF_I_TYPE;
  144. avctx->coded_frame->key_frame = 1;
  145. if(!avctx->codec_tag)
  146. avctx->codec_tag = avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
  147. return 0;
  148. }
  149. static int raw_encode(AVCodecContext *avctx,
  150. unsigned char *frame, int buf_size, void *data)
  151. {
  152. return avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
  153. avctx->height, frame, buf_size);
  154. }
  155. AVCodec rawvideo_encoder = {
  156. "rawvideo",
  157. CODEC_TYPE_VIDEO,
  158. CODEC_ID_RAWVIDEO,
  159. sizeof(AVFrame),
  160. raw_init_encoder,
  161. raw_encode,
  162. };
  163. #endif // CONFIG_RAWVIDEO_ENCODER
  164. AVCodec rawvideo_decoder = {
  165. "rawvideo",
  166. CODEC_TYPE_VIDEO,
  167. CODEC_ID_RAWVIDEO,
  168. sizeof(RawVideoContext),
  169. raw_init_decoder,
  170. NULL,
  171. raw_close_decoder,
  172. raw_decode,
  173. };