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.

135 lines
3.8KB

  1. /*
  2. * MJPEG parser
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2003 Alex Beregszaszi
  5. * Copyright (c) 2003-2004 Michael Niedermayer
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * MJPEG parser.
  26. */
  27. #include "parser.h"
  28. typedef struct MJPEGParserContext{
  29. ParseContext pc;
  30. int size;
  31. }MJPEGParserContext;
  32. /**
  33. * Find the end of the current frame in the bitstream.
  34. * @return the position of the first byte of the next frame, or -1
  35. */
  36. static int find_frame_end(MJPEGParserContext *m, const uint8_t *buf, int buf_size){
  37. ParseContext *pc= &m->pc;
  38. int vop_found, i;
  39. uint32_t state;
  40. vop_found= pc->frame_start_found;
  41. state= pc->state;
  42. i=0;
  43. if(!vop_found){
  44. for(i=0; i<buf_size;){
  45. state= (state<<8) | buf[i];
  46. if(state>=0xFFC00000 && state<=0xFFFEFFFF){
  47. if(state>=0xFFD80000 && state<=0xFFD8FFFF){
  48. i++;
  49. vop_found=1;
  50. break;
  51. }else if(state<0xFFD00000 || state>0xFFD9FFFF){
  52. m->size= (state&0xFFFF)-1;
  53. }
  54. }
  55. if(m->size>0){
  56. int size= FFMIN(buf_size-i, m->size);
  57. i+=size;
  58. m->size-=size;
  59. state=0;
  60. continue;
  61. }else
  62. i++;
  63. }
  64. }
  65. if(vop_found){
  66. /* EOF considered as end of frame */
  67. if (buf_size == 0)
  68. return 0;
  69. for(; i<buf_size;){
  70. state= (state<<8) | buf[i];
  71. if(state>=0xFFC00000 && state<=0xFFFEFFFF){
  72. if(state>=0xFFD80000 && state<=0xFFD8FFFF){
  73. pc->frame_start_found=0;
  74. pc->state=0;
  75. return i-3;
  76. } else if(state<0xFFD00000 || state>0xFFD9FFFF){
  77. m->size= (state&0xFFFF)-1;
  78. }
  79. }
  80. if(m->size>0){
  81. int size= FFMIN(buf_size-i, m->size);
  82. i+=size;
  83. m->size-=size;
  84. state=0;
  85. continue;
  86. }else
  87. i++;
  88. }
  89. }
  90. pc->frame_start_found= vop_found;
  91. pc->state= state;
  92. return END_NOT_FOUND;
  93. }
  94. static int jpeg_parse(AVCodecParserContext *s,
  95. AVCodecContext *avctx,
  96. const uint8_t **poutbuf, int *poutbuf_size,
  97. const uint8_t *buf, int buf_size)
  98. {
  99. MJPEGParserContext *m = s->priv_data;
  100. ParseContext *pc = &m->pc;
  101. int next;
  102. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  103. next= buf_size;
  104. }else{
  105. next= find_frame_end(m, buf, buf_size);
  106. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  107. *poutbuf = NULL;
  108. *poutbuf_size = 0;
  109. return buf_size;
  110. }
  111. }
  112. *poutbuf = buf;
  113. *poutbuf_size = buf_size;
  114. return next;
  115. }
  116. AVCodecParser ff_mjpeg_parser = {
  117. .codec_ids = { AV_CODEC_ID_MJPEG },
  118. .priv_data_size = sizeof(MJPEGParserContext),
  119. .parser_parse = jpeg_parse,
  120. .parser_close = ff_parse_close,
  121. };